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>
32 namespace Tizen { namespace Base { namespace Collection
35 class _MultiHashMapEntry;
39 * @brief This class represents a collection of associated keys and values that are organized based on the hash code of the key.
43 * The %MultiHashMap class represents a collection of associated keys and values that are organized based on the hash code of the key.
44 * There is no limit on the number of elements with the same key, but duplicated elements with the same key are not allowed.
45 * The key and value cannot be @c null.
47 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/base/hashmap_multihashmap.htm">HashMap and MultiHashMap</a>.
49 * The following example demonstrates how to use the %MultiHashMap class.
54 * using namespace Tizen::Base;
55 * using namespace Tizen::Base::Collection;
58 * MyClass::MultiHashMapSample(void)
60 * MultiHashMap map(SingleObjectDeleter);
62 * // Constructs a MultiHashMap instance with default values for capacity, load factor, hash code provider, and comparer
65 * map.Add(new String(L"Zero"), new Integer(0)); // map.GetCount() : 1, map : (Zero -> 0)
66 * map.Add(new String(L"One"), new Integer(1)); // map.GetCount() : 2, map : (Zero -> 0), (one -> 1)
67 * map.Add(new String(L"Two"), new Integer(2)); // map.GetCount() : 3, map : (Zero -> 0), (one -> 1), (Two -> 2)
68 * map.Add(new String(L"Two"), new Integer(20)); // map.GetCount() : 4, map : (Zero -> 0), (One -> 1), (Two -> 2, 20)
70 * // Gets values with the specified key
71 * Integer* pValue = null;
72 * IEnumerator *pValueEnum = map.GetValuesN(String(L"Two"));
73 * while(pValueEnum->MoveNext() == E_SUCCESS)
75 * pValue = static_cast< Integer* > (pValueEnum->GetCurrent());
80 * // Removes values with the specified key // String(L"Two"), Integer(2), Integer(20) are removed
81 * map.Remove(String(L"Two"));
83 * // Uses an enumerator to access elements in the map
84 * IMapEnumerator* pMapEnum = map.GetMapEnumeratorN();
85 * String* pKey = null;
86 * while (pMapEnum->MoveNext() == E_SUCCESS)
88 * pKey = static_cast< String* > (pMapEnum->GetKey());
89 * pValue = static_cast< Integer* > (pMapEnum->GetValue());
94 * // Deallocates all objects
95 * // Because the destructor calls RemoveAll() internally, you do not need to call RemoveAll() to destroy all elements at the end.
100 class _OSP_EXPORT_ MultiHashMap
105 using IMultiMap::Add;
106 using IMultiMap::Remove;
107 using IMultiMap::RemoveAll;
108 using IMultiMap::SetValue;
109 using IMultiMap::Contains;
110 using IMultiMap::ContainsKey;
113 * The object is not fully constructed after this constructor is called. For full construction, @n
114 * the Construct() method must be called right after calling this constructor.
118 * @param[in] deleter The function pointer to type of the element deleter
119 * @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
120 * 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.
121 * It means that you do not transfer the ownership of elements to the collection.
123 * @see SingleObjectDeleter()
124 * @see ArrayDeleter()
126 explicit MultiHashMap(DeleterFunctionType deleter = NoOpDeleter);
129 * This destructor overrides Tizen::Base::Object::~Object().
133 virtual ~MultiHashMap(void);
136 * Initializes a new instance of %MultiHashMap with the specified capacity and load factor.
140 * @return An error code
141 * @param[in] capacity The initial capacity
142 * @param[in] loadFactor The maximum ratio of elements to buckets
143 * @exception E_SUCCESS The method is successful.
144 * @exception E_INVALID_ARG A specified input parameter is invalid, or
145 * the @c capacity or the @c loadFactor is negative.
146 * @remarks The GetHashCode() method of the key object is used for hashing and the
147 * Equals() method of the key object is used for comparing the keys.
148 * @see MultiHashMap()
150 result Construct(int capacity = 16, float loadFactor = 0.75);
153 * Initializes a new instance of %MultiHashMap by copying the elements of the given map.
157 * @return An error code
158 * @param[in] map The map to copy
159 * @param[in] loadFactor The maximum ratio of elements to buckets @n
160 * If it is @c 0, the default load factor(0.75) is used.
161 * @exception E_SUCCESS The method is successful.
162 * @exception E_INVALID_ARG The specified @c loadFactor is negative.
163 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
164 * the @c map is modified during the operation of this method.
165 * @remarks This method performs a shallow copy. It copies just the pointer; not the element itself.
166 * @see MultiHashMap()
168 result Construct(const IMultiMap& map, float loadFactor = 0.75);
172 * Initializes a new instance of the %MultiHashMap class, with the specified initial capacity, load factor, hash code provider, and comparer.
176 * @return An error code
177 * @param[in] capacity The initial capacity @n
178 * If it is @c 0, the default capacity (16) is used.
179 * @param[in] loadFactor The maximum ratio of elements to buckets @n
180 * If it is @c 0, the default load factor (0.75) is used.
181 * @param[in] provider An instance of the IHashCodeProvider derived class that supplies the hash codes
182 * for all keys in this map
183 * @param[in] comparer An instance of the IComparer derived class to use when comparing keys
184 * @exception E_SUCCESS The method is successful.
185 * @exception E_INVALID_ARG A specified input parameter is invalid, or
186 * the @c capacity or the @c loadFactor is negative.
187 * @remarks The instances of hash code provider and comparer will not be deallocated later from this map.
188 * @see MultiHashMap()
190 result Construct(int capacity, float loadFactor, const IHashCodeProvider& provider, const IComparer& comparer);
193 * Initializes a new instance of the %MultiHashMap class by copying the elements of the specified map,
194 * with the specified load factor, hash code provider, and comparer.
198 * @return An error code
199 * @param[in] map A map to copy
200 * @param[in] loadFactor The maximum ratio of elements to buckets @n
201 * If it is @c 0, the default load factor (0.75) is used.
202 * @param[in] provider An instance of the IHashCodeProvider derived class that supplies the hash codes
203 * for all keys in this map
204 * @param[in] comparer An instance of the IComparer derived class to use when comparing keys
205 * @exception E_SUCCESS The method is successful.
206 * @exception E_INVALID_ARG A specified input parameter is invalid, or
207 * the @c loadFactor is negative.
208 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
209 * the @c map is modified during the operation of this method.
210 * @remarks This method performs a shallow copy. It copies just the pointer; not the element itself.
211 * The instances of hash code provider and comparer will not be deallocated later from this map.
212 * @see MultiHashMap()
214 result Construct(const IMultiMap& map, float loadFactor, const IHashCodeProvider& provider, const IComparer& comparer);
217 * Adds the specified key-value pair to this map.
221 * @return An error code
222 * @param[in] pKey The pointer to key to add
223 * @param[in] pValue The pointer to corresponding value to add
224 * @exception E_SUCCESS The method is successful.
225 * @exception E_OBJ_ALREADY_EXIST The specified pair of @c pKey and @c pValue already exists.
226 * @exception E_INVALID_ARG A specified input parameter is invalid, or
227 * the comparer has failed to compare the keys.
228 * @remarks This method performs a shallow copy. It adds just the pointer; not the element itself.
231 virtual result Add(Object* pKey, Object* pValue);
234 * Gets an enumerator of this map.
238 * @return An enumerator (an instance of the IEnumerator derived class) of this map, @n
239 * else @c null if some exception occurs
240 * @remarks If the key has multiple values, the enumeration proceeds as follows: {A: a}, {B: b}, {B: c}, {B, d}, {C: e}, ...
241 * The specific error code can be accessed using the GetLastResult() method.
243 * @see IMapEnumerator()
245 virtual IEnumerator* GetEnumeratorN(void) const;
248 * Gets an enumerator of this map.
252 * @return An enumerator (an instance of the IMapEnumerator derived class) of this map, @n
253 * else @c null if some exception occurs
254 * @remarks If the key has multiple values, the enumeration proceeds as follows: {A: a}, {B: b}, {B: c}, {B, d}, {C: e}, ...
255 * The specific error code can be accessed using the GetLastResult() method.
257 * @see IMapEnumerator()
259 virtual IMapEnumerator* GetMapEnumeratorN(void) const;
262 * Gets an enumerator of the values associated with the specified key.
266 * @return An enumerator (an instance of the IEnumerator derived class) of the values associated with the specified key, @n
267 * else @c null if some exception occurs
268 * @param[in] key A key to locate
269 * @exception E_SUCCESS The method is successful.
270 * @exception E_INVALID_ARG A specified input parameter is invalid, or
271 * the comparer has failed to compare the keys.
272 * @exception E_OBJ_NOT_FOUND The specified @c key is not found in the map.
273 * @remarks The specific error code can be accessed using the GetLastResult() method.
276 virtual IEnumerator* GetValuesN(const Object& key) const;
279 * Gets a list of all unique keys in this map.
283 * @return A list of all unique keys in this map
284 * @remarks The %IList stores just the pointers to the elements in the map, not the elements themselves.
285 * The specific error code can be accessed using the GetLastResult() method.
288 virtual IList* GetKeysN(void) const;
291 * Gets a list of all the values in this map.
295 * @return A list of all the values in this map
296 * @remarks The IList stores just the pointers to the elements in the map, not the elements themselves.
297 * The specific error code can be accessed using the GetLastResult() method.
300 virtual IList* GetValuesN(void) const;
303 * Removes all the values with the specified key.
307 * @return An error code
308 * @param[in] key The key to remove
309 * @exception E_SUCCESS The method is successful.
310 * @exception E_INVALID_ARG A specified input parameter is invalid, or
311 * the comparer has failed to compare keys.
312 * @exception E_OBJ_NOT_FOUND The specified @c key is not found in the map.
315 virtual result Remove(const Object& key);
318 * Removes the specified value associated with the specified key.
322 * @return An error code
323 * @param[in] key The key whose mapping is to remove from the map
324 * @param[in] value The value to remove
325 * @exception E_SUCCESS The method is successful.
326 * @exception E_INVALID_ARG A specified input parameter is invalid, or
327 * the comparer has failed to compare the keys.
328 * @exception E_OBJ_NOT_FOUND The specified @c key and @c value pair is not found in the map.
329 * @remarks The specified key is also removed if there are no more values associated with it.
332 virtual result Remove(const Object& key, const Object& value);
335 * Removes all the object pointers in the @c collection. @n
339 * @remarks This method can be called before deleting @c collection.
341 virtual void RemoveAll(void);
344 * Sets the value associated with the given key with a new value.
348 * @return An error code
349 * @param[in] key The key for which the associated value needs to replace
350 * @param[in] value The value to replace
351 * @param[in] pNewValue The pointer to new value to replace the existing value
352 * @exception E_SUCCESS The method is successful.
353 * @exception E_INVALID_ARG A specified input parameter is invalid, or
354 * the comparer has failed to compare the keys.
355 * @exception E_OBJ_NOT_FOUND The specified @c key and @c value pair is not found in the map.
356 * @remarks To add a new key-value pair, use the Add() method.
360 virtual result SetValue(const Object& key, const Object& value, Object* pNewValue);
363 * Gets the number of values currently stored in this map.
367 * @return The number of values currently stored in this map
369 virtual int GetCount(void) const;
372 * Gets the number of values whose key matches the key.
376 * @return An error code
377 * @param[in] key A key to locate
378 * @param[out] count The number of values whose key is the @c key
379 * @exception E_SUCCESS The method is successful.
380 * @exception E_INVALID_ARG A specified input parameter is invalid, or
381 * the comparer has failed to compare the keys.
382 * @exception E_OBJ_NOT_FOUND The specified @c key is not found in the map.
384 virtual result GetCount(const Object& key, int& count) const;
387 * Checks whether the map contains the specified key and value.
391 * @return @c true if the map contains the specified key and value pair, @n
393 * @param[in] key The key to locate
394 * @param[in] value The value to locate
395 * @exception E_SUCCESS The method is successful.
396 * @exception E_INVALID_ARG A specified input parameter is invalid, or
397 * the comparer has failed to compare the keys.
398 * @remarks The specific error code can be accessed using the GetLastResult() method.
400 * @see ContainsValue()
402 virtual bool Contains(const Object& key, const Object& value) const;
405 * Checks whether the map contains the specified key.
409 * @return @c true if the map contains the specified key, @n
411 * @param[in] key The key to locate
412 * @exception E_SUCCESS The method is successful.
413 * @exception E_INVALID_ARG A specified input parameter is invalid, or
414 * the comparer has failed to compare the keys.
415 * @remarks The specific error code can be accessed using the GetLastResult() method.
416 * @see ContainsValue()
419 virtual bool ContainsKey(const Object& key) const;
422 * Checks whether the map contains the specified value.
426 * @return @c true if the map contains the specified value, @n
428 * @param[in] value The value to locate
433 virtual bool ContainsValue(const Object& value) const;
436 * Compares the specified instance to the current instance for equality.
440 * @return @c true if the two instances are equal, @n
442 * @param[in] obj The object to compare with the current instance
443 * @remarks This method returns @c true only if the specified object is also an instance of %MultiHashMap class,
444 * both maps have the same number of elements, and both maps contain the same elements.
446 virtual bool Equals(const Object& obj) const;
449 * Gets the hash value of the current instance.
453 * @return The hash value of the current instance
454 * @remarks The two Tizen::Base::Object::Equals() instances must return the same hash value. For better performance, @n
455 * the used hash function must generate a random distribution for all inputs.
457 virtual int GetHashCode(void) const;
460 * Gets the element deleter of the collection.
464 * @return A function pointer to the existing element deleter
466 virtual DeleterFunctionType GetDeleter(void) const;
470 * The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
472 * @param[in] map An instance of %MultiHashMap to initialize the current instance
474 MultiHashMap(const MultiHashMap& map);
477 * The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
479 * @param[in] map An instance of %MultiHashMap
481 MultiHashMap& operator =(const MultiHashMap& map);
484 * Copies all the pairs from the specified map to this map.
486 * @return An error code
487 * @param[in] map The map to copy
488 * @exception E_SUCCESS The method is successful.
489 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation. @n
490 * The @c map is modified during the operation of this method.
492 result AddAll(const IMultiMap& map);
495 * Gets a hash value for the specified object.
497 * @return An @c int hash value for the specified object
498 * @param[in] obj The object to get hash value
500 int Hash(const Object& obj) const;
503 * Rehashes the contents of this map into a new array with a
504 * larger capacity. @n
505 * This method is called automatically when the number of keys in this map reaches its threshold.
507 * @return An error code
508 * @param[in] newCapacity The new capacity @n
509 * It must be a power of two and be greater than current capacity.
510 * @exception E_SUCCESS The method is successful.
512 result Resize(int newCapacity);
515 * Clears all key-value pairs in this map.
520 * Sets the element deleter of the collection.
524 * @param[in] deleter A function pointer to the element deleter to set
526 virtual void SetDeleter(DeleterFunctionType deleter);
528 _MultiHashMapEntry** __pTable;
533 IHashCodeProvider* __pProvider;
534 IComparer* __pComparer;
535 bool __needToRemoveProviderComparer;
537 DeleterFunctionType __deleter;
538 static const int DEFAULT_CAPACITY = 16;
539 static const float DEFAULT_LOAD_FACTOR;
541 friend class _MultiHashMapEnumerator;
542 friend class _MultiHashMapImpl;
543 class _MultiHashMapImpl* __pMultiHashMapImpl;
547 }}} // Tizen::Base::Collection
549 #endif //_FBASE_COL_MULTI_HASH_MAP_H_