sipxportlib  Version 3.3
UtlHashBag.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2006 SIPfoundry Inc.
3 // Licensed by SIPfoundry under the LGPL license.
4 //
5 // Copyright (C) 2004-2006 Pingtel Corp. All rights reserved.
6 // Licensed to SIPfoundry under a Contributor Agreement.
7 //
8 // $$
10 
11 
12 #ifndef _UtlHashBag_h_
13 #define _UtlHashBag_h_
14 
15 // SYSTEM INCLUDES
16 // APPLICATION INCLUDES
17 #include "utl/UtlDefs.h"
18 #include "utl/UtlContainer.h"
19 #include "utl/UtlLink.h"
20 
21 // DEFINES
22 // MACROS
23 #if defined(_WIN64) || defined(__x86_64__) || defined(__ppc64__)
24 #define NUM_HASHBAG_BUCKETS(bits) (1ull<<bits)
25 #else
26 #define NUM_HASHBAG_BUCKETS(bits) (1<<bits)
27 #endif
28 
29 // EXTERNAL FUNCTIONS
30 // EXTERNAL VARIABLES
31 // CONSTANTS
32 // STRUCTS
33 // TYPEDEFS
34 // FORWARD DECLARATIONS
35 class UtlContainable;
36 
41 class UtlHashBag : public UtlContainer
42 {
43 /* //////////////////////////// PUBLIC //////////////////////////////////// */
44  public:
46 
47 /* ============================ CREATORS ================================== */
48 
52  UtlHashBag();
53 
57  virtual ~UtlHashBag();
58 /* ============================ MANIPULATORS ============================== */
59 
65  virtual UtlContainable* insert(UtlContainable* object);
66 
72  virtual UtlContainable* remove(UtlContainable* object);
73 
80  virtual UtlContainable* removeReference(const UtlContainable* object);
81 
87  virtual UtlBoolean destroy(UtlContainable* object);
88 
92  virtual void destroyAll();
93 
97  virtual void removeAll();
98 
99 /* ============================ ACCESSORS ================================= */
100 
104  virtual UtlContainable* find(const UtlContainable* object) const;
105 
106 
107 /* ============================ INQUIRY =================================== */
108 
109 
113  size_t entries() const;
114 
118  UtlBoolean isEmpty() const;
119 
125  UtlBoolean contains(const UtlContainable* object) const;
126 
127 
131  virtual UtlContainableType getContainableType() const;
132 
134  size_t numberOfBuckets() const
135  {
137  }
138 
139 /* //////////////////////////// PROTECTED ///////////////////////////////// */
140  protected:
141  friend class UtlHashBagIterator;
142 
143  void notifyIteratorsOfRemove(const UtlLink* pair);
144 
146 
152  {
153  if ( ( mElements / NUM_HASHBAG_BUCKETS(mBucketBits) >= 3 ) // mean bucket 3 or more
154  && ( mIteratorList.isUnLinked() ) /* there are no iterators -
155  * resizing moves elements to new buckets,
156  * which could cause an iterator to miss some
157  * and to return others more than once.
158  */
159  )
160  {
161  resize();
162  }
163  }
164 
165  size_t mElements;
166  size_t mBucketBits;
168 
169 /* //////////////////////////// PRIVATE /////////////////////////////////// */
170  private:
171 
172 
174  void insert(UtlLink* link,
175  UtlChain* bucket
176  );
177 
179  void resize();
184  bool lookup(const UtlContainable* key,
186  UtlChain*& bucket,
189  UtlLink*& pair
191  ) const;
196  void insert(UtlPair* pair,
199  UtlChain* bucket
200  );
201 
203  size_t bucketNumber(unsigned hash) const;
204 
205 
206  // Don't allow the implicit copy constructor.
208 
209  UtlHashBag& operator=(UtlHashBag&);
210 
211 };
212 
213 /* ============================ INLINE METHODS ============================ */
214 
215 
216 #endif // _UtlHashBag_h_
size_t entries() const
Definition: UtlHashBag.cpp:351
virtual UtlContainable * removeReference(const UtlContainable *object)
Definition: UtlHashBag.cpp:216
size_t mElements
number of UtlContainable objects in this UtlHashMap
Definition: UtlHashBag.h:165
virtual UtlContainable * find(const UtlContainable *object) const
Definition: UtlHashBag.cpp:330
UtlBoolean contains(const UtlContainable *object) const
Definition: UtlHashBag.cpp:367
UtlBoolean isEmpty() const
Definition: UtlHashBag.cpp:359
UtlChain * mpBucket
an array of 2**n UtlChain elements, each used as a list header.
Definition: UtlHashBag.h:167
virtual UtlBoolean destroy(UtlContainable *object)
Definition: UtlHashBag.cpp:261
bool isUnLinked() const
Is this block not linked to anything?
Definition: UtlLink.h:93
virtual ~UtlHashBag()
Definition: UtlHashBag.cpp:57
UtlChain mIteratorList
Definition: UtlContainer.h:202
virtual UtlContainable * insert(UtlContainable *object)
Definition: UtlHashBag.cpp:141
const char *const UtlContainableType
Definition: UtlDefs.h:70
Definition: UtlLink.h:49
Definition: UtlContainable.h:37
virtual unsigned hash() const
Definition: UtlContainer.cpp:87
#define NUM_HASHBAG_BUCKETS(bits)
Definition: UtlHashBag.h:26
size_t numberOfBuckets() const
The current number of buckets in the hash.
Definition: UtlHashBag.h:134
virtual void removeAll()
Definition: UtlHashBag.cpp:278
UtlContainable * key() const
Definition: UtlHashBagIterator.cpp:189
Definition: UtlHashBag.h:41
Associate a key object (the parent UtlLink data) with its value object.
Definition: UtlLink.h:457
void resizeIfNeededAndSafe()
If the Hash is too full, add additional buckets.
Definition: UtlHashBag.h:151
int UtlBoolean
Definition: UtlDefs.h:41
Definition: UtlHashBagIterator.h:38
static UtlContainableType TYPE
Definition: UtlHashBag.h:45
void notifyIteratorsOfRemove(const UtlLink *pair)
Definition: UtlHashBag.cpp:390
virtual UtlContainableType getContainableType() const
Definition: UtlHashBag.cpp:381
Definition: UtlContainer.h:38
size_t mBucketBits
number of bits used to index the buckets
Definition: UtlHashBag.h:166
virtual void destroyAll()
Definition: UtlHashBag.cpp:302
UtlHashBag()
Definition: UtlHashBag.cpp:48