Merge refined doxygen comments of Base header files
[platform/framework/native/appfw.git] / inc / FBaseColMultiHashMap.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file                FBaseColMultiHashMap.h
20  * @brief               This is the header file for the %MultiHashMap class.
21  *
22  * This header file contains the declarations of the %MultiHashMap class.
23  */
24 #ifndef _FBASE_COL_MULTI_HASH_MAP_H_
25 #define _FBASE_COL_MULTI_HASH_MAP_H_
26
27 #include <FBaseObject.h>
28 #include <FBaseColIComparer.h>
29 #include <FBaseColIHashCodeProvider.h>
30 #include <FBaseColIMultiMap.h>
31
32
33 namespace Tizen { namespace Base { namespace Collection
34 {
35
36 class _MultiHashMapEntry;
37
38 /**
39  * @class MultiHashMap
40  * @brief This class represents a collection of associated keys and values that are organized based on the hash code of the key.
41  *
42  * @since 2.0
43  *
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.
47  * @n
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>.
49  *
50  * The following example demonstrates how to use the %MultiHashMap class.
51  *
52  * @code
53  *      #include <FBase.h>
54  *
55  *      using namespace Tizen::Base;
56  *      using namespace Tizen::Base::Collection;
57  *
58  *      void
59  *      MyClass::MultiHashMapSample(void)
60  *      {
61  *              MultiHashMap map(SingleObjectDeleter);
62  *
63  *              // Constructs a MultiHashMap instance with default values for capacity, load factor, hash code provider, and comparer
64  *              map.Construct();
65  *
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)
70  *
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)
75  *              {
76  *                      pValue = static_cast< Integer* > (pValueEnum->GetCurrent());
77  *              }
78  *
79  *              delete pValueEnum;
80  *
81  *              // Removes values with the specified key     // String(L"Two"), Integer(2), Integer(20) are removed
82  *              map.Remove(String(L"Two"));
83  *
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)
88  *              {
89  *                      pKey = static_cast< String* > (pMapEnum->GetKey());
90  *                      pValue = static_cast< Integer* > (pMapEnum->GetValue());
91  *              }
92  *
93  *              delete pMapEnum;
94  *
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.
97  *              map.RemoveAll();
98  *      }
99  * @endcode
100  */
101 class _OSP_EXPORT_ MultiHashMap
102         : public IMultiMap
103         , public Object
104 {
105 public:
106         using IMultiMap::Add;
107         using IMultiMap::Remove;
108         using IMultiMap::RemoveAll;
109         using IMultiMap::SetValue;
110         using IMultiMap::Contains;
111         using IMultiMap::ContainsKey;
112
113         /**
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.
116          *
117          * @since 2.0
118          *
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.
123          * @see         NoOpDeleter()
124          * @see         SingleObjectDeleter()
125          * @see         ArrayDeleter()
126          */
127         explicit MultiHashMap(DeleterFunctionType deleter = NoOpDeleter);
128
129         /**
130          * This destructor overrides Tizen::Base::Object::~Object().
131          *
132          * @since 2.0
133          */
134         virtual ~MultiHashMap(void);
135
136         /**
137          * Initializes a new instance of %MultiHashMap with the specified capacity and load factor.
138          *
139          * @since 2.0
140          *
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()
150          */
151         result Construct(int capacity = 16, float loadFactor = 0.75);
152
153         /**
154          * Initializes a new instance of %MultiHashMap by copying the elements of the given map.
155          *
156          * @since 2.0
157          *
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()
168          */
169         result Construct(const IMultiMap& map, float loadFactor = 0.75);
170
171
172         /**
173          * Initializes a new instance of the %MultiHashMap class, with the specified initial capacity, load factor, hash code provider, and comparer.
174          *
175          * @since 2.0
176          *
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()
190          */
191         result Construct(int capacity, float loadFactor, const IHashCodeProvider& provider, const IComparer& comparer);
192
193         /**
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.
196          *
197          * @since 2.0
198          *
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()
214          */
215         result Construct(const IMultiMap& map, float loadFactor, const IHashCodeProvider& provider, const IComparer& comparer);
216
217         /**
218          * Adds the specified key-value pair to this map.
219          *
220          * @since 2.0
221          *
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.
230          * @see                 Remove()
231          */
232         virtual result Add(Object* pKey, Object* pValue);
233
234         /**
235          * Gets an enumerator of this map.
236          *
237          * @since 2.0
238          *
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.
243          * @see                 IEnumerator()
244          * @see                 IMapEnumerator()
245          */
246         virtual IEnumerator* GetEnumeratorN(void) const;
247
248         /**
249          * Gets an enumerator of this map.
250          *
251          * @since 2.0
252          *
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.
257          * @see                 IEnumerator()
258          * @see                 IMapEnumerator()
259          */
260         virtual IMapEnumerator* GetMapEnumeratorN(void) const;
261
262         /**
263          * Gets an enumerator of the values associated with the specified key.
264          *
265          * @since 2.0
266          *
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.
275          * @see                 SetValue()
276          */
277         virtual IEnumerator* GetValuesN(const Object& key) const;
278
279         /**
280          * Gets a list of all unique keys in this map.
281          *
282          * @since 2.0
283          *
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.
287          * @see                 GetValuesN()
288          */
289         virtual IList* GetKeysN(void) const;
290
291         /**
292          * Gets a list of all the values in this map.
293          *
294          * @since 2.0
295          *
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.
299          * @see                 GetKeysN()
300          */
301         virtual IList* GetValuesN(void) const;
302
303         /**
304          * Removes all the values with the specified key.
305          *
306          * @since 2.0
307          *
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.
314          * @see                 Add()
315          */
316         virtual result Remove(const Object& key);
317
318         /**
319          * Removes the specified value associated with the specified key.
320          *
321          * @since 2.0
322          *
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.
331          * @see                 Add()
332          */
333         virtual result Remove(const Object& key, const Object& value);
334
335         /**
336          * Removes all the object pointers in the @c collection. @n
337          *
338          * @since 2.0
339          *
340          * @remarks             This method can be called before deleting @c collection.
341          */
342         virtual void RemoveAll(void);
343
344         /**
345          * Sets the value associated with the given key with a new value.
346          *
347          * @since 2.0
348          *
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.
358          * @see                 Add()
359          * @see                 GetValuesN()
360          */
361         virtual result SetValue(const Object& key, const Object& value, Object* pNewValue);
362
363         /**
364          * Gets the number of values currently stored in this map.
365          *
366          * @since 2.0
367          *
368          * @return              The number of values currently stored in this map
369          */
370         virtual int GetCount(void) const;
371
372         /**
373          * Gets the number of values whose key matches the key.
374          *
375          * @since 2.0
376          *
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.
384          */
385         virtual result GetCount(const Object& key, int& count) const;
386
387         /**
388          * Checks whether the map contains the specified key and value.
389          *
390          * @since 2.0
391          *
392          * @return              @c true if the map contains the specified key and value pair, @n
393          *                              else @c false
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.
400          * @see                 ContainsKey()
401          * @see                 ContainsValue()
402          */
403         virtual bool Contains(const Object& key, const Object& value) const;
404
405         /**
406          * Checks whether the map contains the specified key.
407          *
408          * @since 2.0
409          *
410          * @return              @c true if the map contains the specified key, @n
411          *                              else @c false
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()
418          * @see                 Contains()
419          */
420         virtual bool ContainsKey(const Object& key) const;
421
422         /**
423          * Checks whether the map contains the specified value.
424          *
425          * @since 2.0
426          *
427          * @return              @c true if the map contains the specified value, @n
428          *                              else @c false
429          * @param[in]   value   The value to locate
430          *
431          * @see                         ContainsKey()
432          * @see                         Contains()
433          */
434         virtual bool ContainsValue(const Object& value) const;
435
436         /**
437          * Compares the specified instance to the current instance for equality.
438          *
439          * @since 2.0
440          *
441          * @return              @c true if the two instances are equal, @n
442          *                              @c false
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.
446          */
447         virtual bool Equals(const Object& obj) const;
448
449         /**
450          * Gets the hash value of the current instance.
451          *
452          * @since 2.0
453          *
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.
457          */
458         virtual int GetHashCode(void) const;
459
460         /**
461          * Gets the element deleter of the collection.
462          *
463          * @since 2.0
464          *
465          * @return              A function pointer to the existing element deleter
466          */
467         virtual DeleterFunctionType GetDeleter(void) const;
468
469 private:
470         /**
471          * The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
472          *
473          * @param[in]   map An instance of %MultiHashMap to initialize the current instance
474          */
475         MultiHashMap(const MultiHashMap& map);
476
477         /**
478          * The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
479          *
480          * @param[in]   map An instance of %MultiHashMap
481          */
482         MultiHashMap& operator =(const MultiHashMap& map);
483
484         /**
485          * Copies all the pairs from the specified map to this map.
486          *
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.
492          */
493         result AddAll(const IMultiMap& map);
494
495         /**
496          * Gets a hash value for the specified object.
497          *
498          * @return              An @c int hash value for the specified object
499          * @param[in]   obj     The object to get hash value
500          */
501         int Hash(const Object& obj) const;
502
503         /**
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.
507          *
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.
512          */
513         result Resize(int newCapacity);
514
515         /**
516          * Clears all key-value pairs in this map.
517          */
518         void Reset(void);
519
520         /**
521          * Sets the element deleter of the collection.
522          *
523          * @since 2.0
524          *
525          * @param[in]   deleter A function pointer to the element deleter to set
526          */
527         virtual void SetDeleter(DeleterFunctionType deleter);
528
529         _MultiHashMapEntry** __pTable;
530         int __count;
531         int __capacity;
532         float __loadFactor;
533         int __threshold;
534         IHashCodeProvider* __pProvider;
535         IComparer* __pComparer;
536         bool __needToRemoveProviderComparer;
537         int __modCount;
538         DeleterFunctionType __deleter;
539         static const int DEFAULT_CAPACITY = 16;
540         static const float DEFAULT_LOAD_FACTOR;
541
542         friend class _MultiHashMapEnumerator;
543         friend class _MultiHashMapImpl;
544         class _MultiHashMapImpl* __pMultiHashMapImpl;
545
546 }; // MultiHashMap
547
548 }}} // Tizen::Base::Collection
549
550 #endif //_FBASE_COL_MULTI_HASH_MAP_H_