Merge branch 'master' into jwi-bcc64xsingletonwarning
[ACE_TAO.git] / ACE / apps / JAWS2 / JAWS / Hash_Bucket_T.cpp
blob81d4a0bb6935e7129340bf7fcfba922b4d27dea3
1 #ifndef JAWS_HASH_BUCKET_T_CPP
2 #define JAWS_HASH_BUCKET_T_CPP
4 #include "JAWS/Hash_Bucket_T.h"
6 // -----------------
7 // Hash_Bucket_Item
8 // -----------------
10 template <class EXT_ID, class INT_ID>
11 JAWS_Hash_Bucket_Item<EXT_ID, INT_ID>
12 ::JAWS_Hash_Bucket_Item (const EXT_ID &ext_id, const INT_ID &int_id,
13 JAWS_Hash_Bucket_Item<EXT_ID, INT_ID> *next,
14 JAWS_Hash_Bucket_Item<EXT_ID, INT_ID> *prev)
15 : ext_id_ (ext_id),
16 int_id_ (int_id),
17 next_ (next),
18 prev_ (prev)
22 template <class EXT_ID, class INT_ID>
23 JAWS_Hash_Bucket_Item<EXT_ID, INT_ID>
24 ::JAWS_Hash_Bucket_Item (JAWS_Hash_Bucket_Item<EXT_ID, INT_ID> *next,
25 JAWS_Hash_Bucket_Item<EXT_ID, INT_ID> *prev)
26 : next_ (next),
27 prev_ (prev)
31 template <class EXT_ID, class INT_ID>
32 JAWS_Hash_Bucket_Item<EXT_ID, INT_ID>::~JAWS_Hash_Bucket_Item ()
34 this->next_ = 0;
35 this->prev_ = 0;
39 // ---------------------
40 // Hash_Bucket_DLCStack
41 // ---------------------
43 template <class EXT_ID, class INT_ID>
44 JAWS_Hash_Bucket_DLCStack<EXT_ID, INT_ID>::
45 JAWS_Hash_Bucket_DLCStack (ACE_Allocator *alloc)
46 : allocator_ (alloc),
47 head_ (0),
48 tail_ (0)
50 if (this->allocator_ == 0)
51 this->allocator_ = ACE_Allocator::instance ();
54 template <class EXT_ID, class INT_ID>
55 JAWS_Hash_Bucket_DLCStack<EXT_ID, INT_ID>::
56 ~JAWS_Hash_Bucket_DLCStack ()
58 this->reset ();
61 template <class EXT_ID, class INT_ID> int
62 JAWS_Hash_Bucket_DLCStack<EXT_ID, INT_ID>::
63 is_empty () const
65 return this->head_ == 0 && this->tail_ == 0;
68 template <class EXT_ID, class INT_ID> JAWS_HASH_BUCKET_ITEM *
69 JAWS_Hash_Bucket_DLCStack<EXT_ID, INT_ID>::
70 push (const EXT_ID &ext_id, const INT_ID &int_id)
72 size_t malloc_size = sizeof (JAWS_HASH_BUCKET_ITEM);
73 JAWS_HASH_BUCKET_ITEM *item;
74 ACE_NEW_MALLOC_RETURN (item,
75 (JAWS_HASH_BUCKET_ITEM *)
76 this->allocator_->malloc (malloc_size),
77 JAWS_HASH_BUCKET_ITEM (ext_id, int_id), 0);
79 if (item != 0)
81 if (this->is_empty ())
83 this->head_ = item;
84 this->tail_ = item;
85 item->next_ = this->head_;
86 item->prev_ = this->tail_;
88 else
90 item->next_ = this->head_;
91 item->prev_ = this->tail_;
92 this->head_->prev_ = item;
93 this->tail_->next_ = item;
94 this->head_ = item;
98 return item;
101 template <class EXT_ID, class INT_ID> JAWS_HASH_BUCKET_ITEM *
102 JAWS_Hash_Bucket_DLCStack<EXT_ID, INT_ID>::pop ()
104 JAWS_HASH_BUCKET_ITEM *item = 0;
106 if (! this->is_empty ())
108 item = this->head_;
109 if (this->head_ == this->tail_)
111 this->head_ = this->tail_ = 0;
113 else
115 this->head_ = this->head_->next_;
116 this->head_->prev_ = this->tail_;
117 this->tail_->next_ = this->head_;
119 item->next_ = 0;
120 item->prev_ = 0;
123 return item;
126 template <class EXT_ID, class INT_ID> void
127 JAWS_Hash_Bucket_DLCStack<EXT_ID, INT_ID>::reset ()
129 JAWS_HASH_BUCKET_ITEM *item = 0;
131 while ((item = this->pop ()) != 0)
132 this->remove (item);
135 template <class EXT_ID, class INT_ID> int
136 JAWS_Hash_Bucket_DLCStack<EXT_ID, INT_ID>::remove (JAWS_HASH_BUCKET_ITEM *item)
138 int result = 0;
140 if (item != 0)
142 if (item->next_ != 0 && item->prev_ != 0)
144 if (item->next_ != item)
146 if (this->head_ == item)
147 this->head_ = item->next_;
148 if (this->tail_ == item)
149 this->tail_ = item->prev_;
150 item->next_->prev_ = item->prev_;
151 item->prev_->next_ = item->next_;
153 else
155 this->head_ = this->tail_ = 0;
157 item->next_ = 0;
158 item->prev_ = 0;
161 if (item->next_ == 0 && item->prev_ == 0)
163 ACE_DES_FREE_TEMPLATE2 (item, this->allocator_->free,
164 JAWS_Hash_Bucket_Item, EXT_ID, INT_ID);
166 else
167 result = -1;
170 return result;
174 // ------------------------------
175 // Hash_Bucket_DLCStack_Iterator
176 // ------------------------------
178 template <class EXT_ID, class INT_ID>
179 JAWS_Hash_Bucket_DLCStack_Iterator<EXT_ID, INT_ID>::
180 JAWS_Hash_Bucket_DLCStack_Iterator (const JAWS_HASH_BUCKET_DLCSTACK &dlcstack)
181 : dlcstack_ (dlcstack),
182 next_ (0),
183 prev_ (0),
184 done_ (0)
188 template <class EXT_ID, class INT_ID> int
189 JAWS_Hash_Bucket_DLCStack_Iterator<EXT_ID, INT_ID>::first ()
191 int result = 0;
193 if (! this->dlcstack_.is_empty ())
195 result = 1;
196 this->next_ = this->dlcstack_.head_;
197 this->prev_ = this->dlcstack_.tail_;
198 this->done_ = 0;
201 return result;
204 template <class EXT_ID, class INT_ID> int
205 JAWS_Hash_Bucket_DLCStack_Iterator<EXT_ID, INT_ID>::last ()
207 return this->first ();
210 template <class EXT_ID, class INT_ID> int
211 JAWS_Hash_Bucket_DLCStack_Iterator<EXT_ID, INT_ID>::advance ()
213 int result = 1;
215 if (this->next_ != 0)
217 this->prev_ = this->next_;
218 this->next_ = this->next_->next_;
219 if (this->next_ == this->dlcstack_.head_)
221 this->done_ = 1;
222 result = 0;
225 else
226 result = this->first ();
228 return result;
231 template <class EXT_ID, class INT_ID> int
232 JAWS_Hash_Bucket_DLCStack_Iterator<EXT_ID, INT_ID>::revert ()
234 int result = 1;
236 if (this->prev_ != 0)
238 this->next_ = this->prev_;
239 this->prev_ = this->prev_->prev_;
240 if (this->prev_ == this->dlcstack_.tail_)
242 this->done_ = 1;
243 result = 0;
246 else
247 result = this->last ();
249 return result;
252 template <class EXT_ID, class INT_ID> int
253 JAWS_Hash_Bucket_DLCStack_Iterator<EXT_ID, INT_ID>::
254 next (JAWS_HASH_BUCKET_ITEM *&item)
256 if (this->next_ == 0)
257 this->first ();
259 item = this->next_;
260 return ! this->done ();
263 template <class EXT_ID, class INT_ID> int
264 JAWS_Hash_Bucket_DLCStack_Iterator<EXT_ID, INT_ID>::
265 next (JAWS_HASH_BUCKET_ITEM *&item) const
267 item = this->next_;
268 return ! this->done ();
271 template <class EXT_ID, class INT_ID> int
272 JAWS_Hash_Bucket_DLCStack_Iterator<EXT_ID, INT_ID>::
273 prev (JAWS_HASH_BUCKET_ITEM *&item)
275 if (this->prev_ == 0)
276 this->last ();
278 item = this->prev_;
279 return ! this->done ();
282 template <class EXT_ID, class INT_ID> int
283 JAWS_Hash_Bucket_DLCStack_Iterator<EXT_ID, INT_ID>::
284 prev (JAWS_HASH_BUCKET_ITEM *&item) const
286 item = this->prev_;
287 return ! this->done ();
290 template <class EXT_ID, class INT_ID> int
291 JAWS_Hash_Bucket_DLCStack_Iterator<EXT_ID, INT_ID>::done () const
293 return this->done_;
297 // --------------------
298 // Hash_Bucket_Manager
299 // --------------------
301 template <class EXT_ID, class INT_ID, class EQ_FUNC>
302 JAWS_Hash_Bucket_Manager<EXT_ID,INT_ID,EQ_FUNC>
303 ::JAWS_Hash_Bucket_Manager (ACE_Allocator *alloc)
304 : dlcstack_ (alloc)
306 if (alloc == 0)
307 this->dlcstack_.allocator_ = ACE_Allocator::instance ();
310 template <class EXT_ID, class INT_ID, class EQ_FUNC> int
311 JAWS_Hash_Bucket_Manager<EXT_ID,INT_ID,EQ_FUNC>::open (ACE_Allocator *alloc)
313 this->dlcstack_.allocator_ = alloc;
314 if (alloc == 0)
315 this->dlcstack_.allocator_ = ACE_Allocator::instance ();
317 return 0;
320 template <class EXT_ID, class INT_ID, class EQ_FUNC>
321 JAWS_Hash_Bucket_Manager<EXT_ID,INT_ID,EQ_FUNC>::~JAWS_Hash_Bucket_Manager ()
325 template <class EXT_ID, class INT_ID, class EQ_FUNC> int
326 JAWS_Hash_Bucket_Manager<EXT_ID,INT_ID,EQ_FUNC>::close ()
328 this->dlcstack_.reset ();
329 return 0;
332 template <class EXT_ID, class INT_ID, class EQ_FUNC> JAWS_HASH_BUCKET_ITEM *
333 JAWS_Hash_Bucket_Manager<EXT_ID,INT_ID,EQ_FUNC>
334 ::find_i (const EXT_ID &ext_id) const
336 JAWS_HASH_BUCKET_DLCSTACK_ITERATOR iter (this->dlcstack_);
337 JAWS_HASH_BUCKET_ITEM *item = 0;
339 if (iter.first ())
340 while (!iter.done ())
342 iter.next (item);
343 if (item && EQ_FUNC (item->ext_id_, ext_id))
344 break;
345 iter.advance ();
348 return (item && EQ_FUNC (item->ext_id_, ext_id)) ? item : 0;
351 template <class EXT_ID, class INT_ID, class EQ_FUNC> int
352 JAWS_Hash_Bucket_Manager<EXT_ID,INT_ID,EQ_FUNC>::find (const EXT_ID &ext_id,
353 INT_ID &int_id) const
355 int result = -1;
356 JAWS_HASH_BUCKET_ITEM *item = this->find_i (ext_id);
358 if (item)
360 int_id = item->int_id_;
361 result = 0;
364 return result;
367 template <class EXT_ID, class INT_ID, class EQ_FUNC> int
368 JAWS_Hash_Bucket_Manager<EXT_ID,INT_ID,EQ_FUNC>
369 ::find (const EXT_ID &ext_id) const
371 INT_ID dummy_id;
372 return this->find (ext_id, dummy_id);
375 template <class EXT_ID, class INT_ID, class EQ_FUNC> int
376 JAWS_Hash_Bucket_Manager<EXT_ID,INT_ID,EQ_FUNC>::bind (const EXT_ID &ext_id,
377 const INT_ID &int_id)
379 int result = 0;
381 if (this->find (ext_id) == 0)
383 result = 1;
385 else
387 if (this->dlcstack_.push (ext_id, int_id) == 0)
388 result = -1;
391 return result;
394 template <class EXT_ID, class INT_ID, class EQ_FUNC> int
395 JAWS_Hash_Bucket_Manager<EXT_ID,INT_ID,EQ_FUNC>::trybind (const EXT_ID &ext_id,
396 INT_ID &int_id)
398 int result = 0;
400 if (this->find (ext_id, int_id) == 0)
402 result = 1;
404 else
406 if (this->dlcstack_.push (ext_id, int_id) == 0)
407 result = -1;
410 return result;
413 template <class EXT_ID, class INT_ID, class EQ_FUNC> int
414 JAWS_Hash_Bucket_Manager<EXT_ID,INT_ID,EQ_FUNC>::rebind (const EXT_ID &ext_id,
415 const INT_ID &int_id,
416 EXT_ID &old_ext_id,
417 INT_ID &old_int_id)
419 int result = 0;
420 JAWS_HASH_BUCKET_ITEM *item = this->find_i (ext_id);
422 if (item)
424 result = 1;
425 old_ext_id = item->ext_id_;
426 old_int_id = item->int_id_;
427 this->dlcstack_.remove (item);
430 if (this->dlcstack_.push (ext_id, int_id) == 0)
431 result = -1;
433 return result;
436 template <class EXT_ID, class INT_ID, class EQ_FUNC> int
437 JAWS_Hash_Bucket_Manager<EXT_ID,INT_ID,EQ_FUNC>::unbind (const EXT_ID &ext_id,
438 INT_ID &int_id)
440 int result = -1;
441 JAWS_HASH_BUCKET_ITEM *item = this->find_i (ext_id);
443 if (item)
445 result = 0;
446 int_id = item->int_id_;
447 this->dlcstack_.remove (item);
450 return result;
453 template <class EXT_ID, class INT_ID, class EQ_FUNC> int
454 JAWS_Hash_Bucket_Manager<EXT_ID,INT_ID,EQ_FUNC>::unbind (const EXT_ID &ext_id)
456 INT_ID dummy_id;
457 return this->unbind (ext_id, dummy_id);
460 #endif /* JAWS_HASH_BUCKET_T_CPP */