sipxportlib  Version 3.3
UtlHashMap.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2008-2011 SIPez LLC. All rights reserved.
3 // Licensed to SIPfoundry under a Contributor Agreement.
4 //
5 // Copyright (C) 2004-2008 SIPfoundry Inc.
6 // Licensed by SIPfoundry under the LGPL license.
7 //
8 // Copyright (C) 2004-2006 Pingtel Corp. All rights reserved.
9 // Licensed to SIPfoundry under a Contributor Agreement.
10 //
11 // $$
13 
14 
15 #ifndef _UtlHashMap_h_
16 #define _UtlHashMap_h_
17 
18 // SYSTEM INCLUDES
19 // APPLICATION INCLUDES
20 #include "utl/UtlDefs.h"
21 #include "utl/UtlContainer.h"
22 
23 // DEFINES
24 // MACROS
25 #if defined(_WIN64) || defined(__x86_64__) || defined(__ppc64__)
26 #define NUM_HASHMAP_BUCKETS(bits) (1ull<<bits)
27 #else
28 #define NUM_HASHMAP_BUCKETS(bits) (1<<bits)
29 #endif
30 
31 // EXTERNAL FUNCTIONS
32 // EXTERNAL VARIABLES
33 // CONSTANTS
34 // STRUCTS
35 // TYPEDEFS
36 // FORWARD DECLARATIONS
37 class UtlContainable;
38 class UtlPair;
39 
46 class UtlHashMap : public UtlContainer
47 {
48 /* //////////////////////////// PUBLIC //////////////////////////////////// */
49 public:
50  static const UtlContainableType TYPE;
51 
52 /* ============================ CREATORS ================================== */
53 
55  UtlHashMap();
56 
58  virtual ~UtlHashMap();
59 
61  virtual UtlHashMap* clone() const;
62 
63 /* ============================ MANIPULATORS ============================== */
64 
91  UtlContainable* remove(UtlContainable* key);
113  virtual UtlBoolean destroy(UtlContainable* key);
123  virtual void destroyAll();
125 
126 
128  virtual void removeAll();
129 
130 /* ============================ ACCESSORS ================================= */
131 
133  UtlContainable* findValue(const UtlContainable* key) const;
134 
135 
137  virtual UtlContainable* find(const UtlContainable* key) const;
138 
139 /* ============================ INQUIRY =================================== */
140 
142  size_t entries() const;
143 
144 
146  UtlBoolean isEmpty() const;
147 
148 
150  UtlBoolean contains(const UtlContainable* key) const;
151 
152 
154  virtual UtlContainableType getContainableType() const;
155 
158  void copyInto(UtlHashMap& map) const;
164  OsStatus deepCopyInto(UtlHashMap& map) const;
180  size_t numberOfBuckets() const
182  {
184  }
185 
186 
187 /* //////////////////////////// PROTECTED ///////////////////////////////// */
188 protected:
189  friend class UtlHashMapIterator;
190 
192 
194  inline
195  void resizeIfNeededAndSafe();
202  size_t mElements;
203  size_t mBucketBits;
205 
206 /* //////////////////////////// PRIVATE /////////////////////////////////// */
207 private:
208 
210  void resize();
215  bool lookup(const UtlContainable* key,
217  UtlChain*& bucket,
218  UtlPair*& pair) const;
229  void insert(UtlPair* pair,
231  UtlChain* bucket);
238  size_t bucketNumber(unsigned hash) const;
240 
243 
245  UtlHashMap& operator=(const UtlHashMap&);
246 
248  void notifyIteratorsOfRemove(const UtlPair* pair);
249 };
250 
252 {
253  // IF mean bucket 3 or more AND there are no iterators.
254  // Resizing moves elements to new buckets, which could cause an iterator
255  // to miss some and to return others more than once.
256  if ( ( mElements / NUM_HASHMAP_BUCKETS(mBucketBits) >= 3 )
257  && ( mIteratorList.isUnLinked() ))
258  {
259  resize();
260  }
261 }
262 
263 #endif // _UtlHashMap_h_
UtlContainable * insert(UtlContainable *obj)
Inserts the designated containable object into the list with a NULL value.
Definition: UtlHashMap.cpp:241
virtual UtlContainableType getContainableType() const
Get the ContainableType for the hash bag as a contained object.
Definition: UtlHashMap.cpp:419
size_t numberOfBuckets() const
The current number of buckets in the hash.
Definition: UtlHashMap.h:181
void copyInto(UtlHashMap &map) const
Make a copy of all of the items BY POINTER in (*this) instance into the given map.
Definition: UtlHashMap.cpp:314
static const UtlContainable * INTERNAL_NULL
Definition: UtlHashMap.h:191
bool isUnLinked() const
Is this block not linked to anything?
Definition: UtlLink.h:93
OsStatus
Definition: OsStatus.h:27
Definition: UtlHashMapIterator.h:38
virtual UtlHashMap * clone() const
Make an off the heap copy of this object.
Definition: UtlHashMap.cpp:135
UtlChain mIteratorList
Definition: UtlContainer.h:202
virtual void destroyAll()
Removes all elements from the hash map and deletes each element.
Definition: UtlHashMap.cpp:211
const char *const UtlContainableType
Definition: UtlDefs.h:70
Definition: UtlLink.h:49
Definition: UtlContainable.h:37
virtual void removeAll()
Removes all elements from the hash map without deleting the elements.
Definition: UtlHashMap.cpp:187
size_t mBucketBits
number of bits used to index the buckets
Definition: UtlHashMap.h:203
virtual UtlContainable * find(const UtlContainable *key) const
Return the designated key if found otherwise NULL.
Definition: UtlHashMap.cpp:376
virtual unsigned hash() const
Definition: UtlContainer.cpp:87
virtual ~UtlHashMap()
Destructor.
Definition: UtlHashMap.cpp:53
UtlContainable * findValue(const UtlContainable *key) const
Return the value for a given key or NULL if not found.
Definition: UtlHashMap.cpp:358
virtual UtlBoolean destroy(UtlContainable *key)
Removes the designated key and its associated value from the map and frees the key and the value (if ...
Definition: UtlHashMap.cpp:164
UtlBoolean isEmpty() const
Return true if the hash map is empty (entries() == 0), otherwise false.
Definition: UtlHashMap.cpp:404
UtlHashMap()
Default Constructor.
Definition: UtlHashMap.cpp:44
Definition: UtlHashMap.h:46
Associate a key object (the parent UtlLink data) with its value object.
Definition: UtlLink.h:457
UtlContainable * key() const
Definition: UtlHashMapIterator.cpp:130
size_t mElements
number of UtlContainable objects in this UtlHashMap
Definition: UtlHashMap.h:202
int UtlBoolean
Definition: UtlDefs.h:41
UtlContainable * removeReference(const UtlContainable *key)
Remove the designated key and its associated value.
Definition: UtlHashMap.cpp:153
UtlBoolean contains(const UtlContainable *key) const
Return true if the hash map includes an entry with the specified key.
Definition: UtlHashMap.cpp:410
Definition: UtlContainer.h:38
UtlContainable * removeKeyAndValue(const UtlContainable *key, UtlContainable *&value)
Remove the designated key and its associated value.
Definition: UtlHashMap.cpp:282
#define NUM_HASHMAP_BUCKETS(bits)
Definition: UtlHashMap.h:28
UtlContainable * insertKeyAndValue(UtlContainable *key, UtlContainable *value)
Inserts a key and value pair into the hash map.
Definition: UtlHashMap.cpp:248
OsStatus deepCopyInto(UtlHashMap &map) const
Definition: UtlHashMap.cpp:323
void resizeIfNeededAndSafe()
If the Hash is too full, add additional buckets.
Definition: UtlHashMap.h:251
static const UtlContainableType TYPE
Definition: UtlHashMap.h:50
size_t entries() const
Return the total number of keys in the hash map.
Definition: UtlHashMap.cpp:396
UtlChain * mpBucket
an array of 2**n UtlChain elements, each used as a list header.
Definition: UtlHashMap.h:204