2 // Hey Emacs! This is a C++ file!
3 #ifndef JAWS_HASH_BUCKET_T_H
4 #define JAWS_HASH_BUCKET_T_H
6 #include "ace/Containers.h"
8 #define JAWS_HASH_BUCKET_ITEM JAWS_Hash_Bucket_Item<EXT_ID, INT_ID>
9 #define JAWS_HASH_BUCKET_DLCSTACK JAWS_Hash_Bucket_DLCStack<EXT_ID, INT_ID>
10 #define JAWS_HASH_BUCKET_DLCSTACK_ITERATOR \
11 JAWS_Hash_Bucket_DLCStack_Iterator<EXT_ID, INT_ID>
16 // This is an attempt to simplify the creation of high-performance
17 // hash tables with respect to concurrent access by multiple threads.
18 // To this end, we attempt to raise the amount of concurrency through
19 // the use or readers/writer locks rather than through mutual
22 template <class EXT_ID
, class INT_ID
>
23 class JAWS_Hash_Bucket_Item
26 JAWS_Hash_Bucket_Item (const EXT_ID
&ext_id
, const INT_ID
&int_id
,
27 JAWS_Hash_Bucket_Item
<EXT_ID
, INT_ID
> *next
= 0,
28 JAWS_Hash_Bucket_Item
<EXT_ID
, INT_ID
> *prev
= 0);
29 JAWS_Hash_Bucket_Item (JAWS_Hash_Bucket_Item
<EXT_ID
, INT_ID
> *next
= 0,
30 JAWS_Hash_Bucket_Item
<EXT_ID
, INT_ID
> *prev
= 0);
32 ~JAWS_Hash_Bucket_Item (void);
36 // Key used to look up an entry.
39 // The contents of the entry itself.
41 JAWS_Hash_Bucket_Item
<EXT_ID
, INT_ID
> *next_
;
42 // Pointer to the next item in the bucket of overflow nodes.
44 JAWS_Hash_Bucket_Item
<EXT_ID
, INT_ID
> *prev_
;
45 // Pointer to the prev item in the bucket of overflow nodes.
50 template <class EXT_ID
, class INT_ID
> class JAWS_Hash_Bucket_DLCStack_Iterator
;
52 template <class EXT_ID
, class INT_ID
, class EQ_FUNC
>
53 class JAWS_Hash_Bucket_Manager
;
55 template <class EXT_ID
, class INT_ID
>
56 class JAWS_Hash_Bucket_DLCStack
57 // Create a doubly linked circular stack to be managed by the
58 // Hash_Bucket_Manager
60 friend class JAWS_Hash_Bucket_DLCStack_Iterator
<EXT_ID
, INT_ID
>;
64 JAWS_Hash_Bucket_DLCStack (ACE_Allocator
*alloc
= 0);
65 ~JAWS_Hash_Bucket_DLCStack (void);
67 int is_empty (void) const;
68 // Returns 1 if the container is empty, otherwise returns 0.
70 JAWS_Hash_Bucket_Item
<EXT_ID
, INT_ID
> *push (const EXT_ID
&ext_id
,
71 const INT_ID
&int_id
);
72 // Adds <new_item> to the head of the list.
73 // Returns the new item that was inserted.
75 JAWS_Hash_Bucket_Item
<EXT_ID
, INT_ID
> *pop (void);
76 // Removes and returns the first <item> in the list. Returns
77 // internal node's address on success, 0 if the queue was empty.
78 // This method will *not* free the internal node.
81 // Reset the <JAWS_Hash_Bucket_DLCStack> to be empty.
82 // Notice that since no one is interested in the items within,
83 // This operation will delete all items.
85 int remove (JAWS_Hash_Bucket_Item
<EXT_ID
, INT_ID
> *item
);
86 // If item is still part of the CStack, it is removed.
87 // In anycase, if there is no error, item is freed.
88 // Returns 0 if ok, -1 on error.
90 ACE_Allocator
*allocator_
;
94 JAWS_Hash_Bucket_Item
<EXT_ID
, INT_ID
> *head_
;
95 JAWS_Hash_Bucket_Item
<EXT_ID
, INT_ID
> *tail_
;
100 template <class EXT_ID
, class INT_ID
>
101 class JAWS_Hash_Bucket_DLCStack_Iterator
105 JAWS_Hash_Bucket_DLCStack_Iterator (const JAWS_HASH_BUCKET_DLCSTACK
&dlcstack
);
108 // Moves to first element in the set, clears done flag. Returns 0
109 // if empty, 1 otherwise.
112 // Moves to last element in the set, clears done flag. Returns 0 if
113 // empty, 1 otherwise.
116 // Move forward by one element of set. Returns 0 if empty or we end
117 // up being the first element in the set, 1 otherwise. If advance
118 // takes us to the first element, done is set to true.
121 // Move backward by one element of set. Returns 0 if empty or we
122 // end up being the last element in the set, 1 otherwise. If revert
123 // takes us to the last element, done is set to true.
125 int next (JAWS_HASH_BUCKET_ITEM
*&item
);
126 int next (JAWS_HASH_BUCKET_ITEM
*&item
) const;
127 // Pass back the next item. Returns 0 if done is true, 1 otherwise.
129 int prev (JAWS_HASH_BUCKET_ITEM
*&item
);
130 int prev (JAWS_HASH_BUCKET_ITEM
*&item
) const;
131 // Pass back the previous item. Returns 0 if done is true, 1
134 int done (void) const;
135 // Returns 1 if done_ flag is set, 0 otherwise. done_ flag is set
136 // if next takes us to first element or prev takes us to last
140 const JAWS_HASH_BUCKET_DLCSTACK
&dlcstack_
;
141 JAWS_HASH_BUCKET_ITEM
*next_
;
142 JAWS_HASH_BUCKET_ITEM
*prev_
;
147 template <class EXT_ID
, class INT_ID
, class EQ_FUNC
>
148 class JAWS_Hash_Bucket_Manager
151 JAWS_Hash_Bucket_Manager (ACE_Allocator
*alloc
= 0);
152 int open (ACE_Allocator
*alloc
= 0);
154 ~JAWS_Hash_Bucket_Manager (void);
157 int find (const EXT_ID
&ext_id
) const;
158 int find (const EXT_ID
&ext_id
, INT_ID
&int_id
) const;
159 // Locate <ext_id> and pass out parameter via <int_id>. If found,
160 // return 0, returns -1 if not found.
162 int bind (const EXT_ID
&ext_id
, const INT_ID
&int_id
);
163 int trybind (const EXT_ID
&ext_id
, INT_ID
&int_id
);
164 // Associate <ext_id> with <int_id> if and only if <ext_id> is not
165 // in the map. If <ext_id> is already in the map then the <int_id>
166 // parameter is assigned the existing value in the map. Returns 0
167 // if a new entry is bound successfully, returns 1 if an attempt is
168 // made to bind an existing entry, and returns -1 if failures occur.
170 int rebind (const EXT_ID
&ext_id
, const INT_ID
&int_id
,
171 EXT_ID
&old_ext_id
, INT_ID
&old_int_id
);
172 // Associate <ext_id> with <int_id>. If <ext_id> is not in the map
173 // then behaves just like <bind>. Otherwise, store the old values
174 // of <ext_id> and <int_id> into the "out" parameters and rebind the
175 // new parameters. This is very useful if you need to have an
176 // atomic way of updating <JAWS_Hash_Map_Entrys> and you also need full
177 // control over memory allocation. Returns 0 if a new entry is
178 // bound successfully, returns 1 if an existing entry was rebound,
179 // and returns -1 if failures occur.
181 int unbind (const EXT_ID
&ext_id
);
182 int unbind (const EXT_ID
&ext_id
, INT_ID
&int_id
);
183 // Break any association of <ext_id>. Returns the value of <int_id>
184 // in case the caller needs to deallocate memory. Return value is 0
185 // if unbind succeeds, -1 otherwise.
189 JAWS_Hash_Bucket_Item
<EXT_ID
, INT_ID
> *find_i (const EXT_ID
&ext_id
) const;
190 // Returns the item associated with ext_id if found in list.
191 // Returns NULL if not found.
195 JAWS_Hash_Bucket_DLCStack
<EXT_ID
, INT_ID
> dlcstack_
;
200 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
201 #include "JAWS/Hash_Bucket_T.cpp"
202 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
204 #endif /* JAWS_HASH_BUCKET_T_H */