00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef ACE_HASH_MAP_MANAGER_T_CPP
00014 #define ACE_HASH_MAP_MANAGER_T_CPP
00015
00016 #include "ace/Hash_Map_Manager_T.h"
00017
00018 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00019 # pragma once
00020 #endif
00021
00022 #if !defined (__ACE_INLINE__)
00023 # include "ace/Hash_Map_Manager_T.inl"
00024 #endif
00025
00026 #include "ace/Malloc_Base.h"
00027
00028 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00029
00030 template <class EXT_ID, class INT_ID>
00031 ACE_Hash_Map_Entry<EXT_ID, INT_ID>::ACE_Hash_Map_Entry (ACE_Hash_Map_Entry<EXT_ID, INT_ID> *next,
00032 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *prev)
00033 : next_ (next),
00034 prev_ (prev)
00035 {
00036 }
00037
00038 template <class EXT_ID, class INT_ID>
00039 ACE_Hash_Map_Entry<EXT_ID, INT_ID>::ACE_Hash_Map_Entry (const EXT_ID &ext_id,
00040 const INT_ID &int_id,
00041 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *next,
00042 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *prev)
00043 : ext_id_ (ext_id),
00044 int_id_ (int_id),
00045 next_ (next),
00046 prev_ (prev)
00047 {
00048 }
00049
00050 template <class EXT_ID, class INT_ID>
00051 ACE_Hash_Map_Entry<EXT_ID, INT_ID>::~ACE_Hash_Map_Entry (void)
00052 {
00053 }
00054
00055 template <class EXT_ID, class INT_ID> EXT_ID &
00056 ACE_Hash_Map_Entry<EXT_ID, INT_ID>::key ()
00057 {
00058 return ext_id_;
00059 }
00060
00061 template <class EXT_ID, class INT_ID> INT_ID &
00062 ACE_Hash_Map_Entry<EXT_ID, INT_ID>::item ()
00063 {
00064 return int_id_;
00065 }
00066
00067 template <class EXT_ID, class INT_ID> void
00068 ACE_Hash_Map_Entry<EXT_ID, INT_ID>::dump (void) const
00069 {
00070 #if defined (ACE_HAS_DUMP)
00071 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00072 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("next_ = %d"), this->next_));
00073 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("prev_ = %d"), this->prev_));
00074 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00075 #endif
00076 }
00077
00078 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> void
00079 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::dump (void) const
00080 {
00081 #if defined (ACE_HAS_DUMP)
00082 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00083 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("total_size_ = %d"), this->total_size_));
00084 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\ncur_size_ = %d"), this->cur_size_));
00085 this->table_allocator_->dump ();
00086 this->entry_allocator_->dump ();
00087 this->lock_.dump ();
00088 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00089 #endif
00090 }
00091
00092 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00093 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::create_buckets (size_t size)
00094 {
00095 size_t bytes = size * sizeof (ACE_Hash_Map_Entry<EXT_ID, INT_ID>);
00096 void *ptr;
00097
00098 ACE_ALLOCATOR_RETURN (ptr,
00099 this->table_allocator_->malloc (bytes),
00100 -1);
00101
00102 this->table_ = (ACE_Hash_Map_Entry<EXT_ID, INT_ID> *) ptr;
00103
00104 this->total_size_ = size;
00105
00106
00107
00108
00109 for (size_t i = 0; i < size; i++)
00110 new (&this->table_[i]) ACE_Hash_Map_Entry<EXT_ID, INT_ID> (&this->table_[i],
00111 &this->table_[i]);
00112 return 0;
00113 }
00114
00115 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00116 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::open (size_t size,
00117 ACE_Allocator *table_alloc,
00118 ACE_Allocator *entry_alloc)
00119 {
00120 ACE_WRITE_GUARD_RETURN (ACE_LOCK, ace_mon, this->lock_, -1);
00121
00122
00123
00124 this->close_i ();
00125
00126 if (table_alloc == 0)
00127 table_alloc = ACE_Allocator::instance ();
00128
00129 this->table_allocator_ = table_alloc;
00130
00131 if (entry_alloc == 0)
00132 entry_alloc = table_alloc;
00133
00134 this->entry_allocator_ = entry_alloc;
00135
00136
00137
00138
00139
00140 if (size == 0)
00141 return -1;
00142
00143 return this->create_buckets (size);
00144 }
00145
00146 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00147 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::close_i (void)
00148 {
00149
00150
00151 if (this->table_ != 0)
00152 {
00153
00154 this->unbind_all_i ();
00155
00156
00157 for (size_t i = 0; i < this->total_size_; i++)
00158 {
00159
00160 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *entry = &this->table_[i];
00161
00162
00163
00164 ACE_DES_FREE_TEMPLATE2 (entry, ACE_NOOP,
00165 ACE_Hash_Map_Entry, EXT_ID, INT_ID);
00166 }
00167
00168
00169 this->total_size_ = 0;
00170
00171
00172 this->table_allocator_->free (this->table_);
00173
00174
00175 this->table_ = 0;
00176 }
00177
00178 return 0;
00179 }
00180
00181 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00182 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::unbind_all_i (void)
00183 {
00184
00185
00186 for (size_t i = 0; i < this->total_size_; i++)
00187 {
00188 for (ACE_Hash_Map_Entry<EXT_ID, INT_ID> *temp_ptr = this->table_[i].next_;
00189 temp_ptr != &this->table_[i];
00190 )
00191 {
00192 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *hold_ptr = temp_ptr;
00193 temp_ptr = temp_ptr->next_;
00194
00195
00196 ACE_DES_FREE_TEMPLATE2 (hold_ptr, this->entry_allocator_->free,
00197 ACE_Hash_Map_Entry, EXT_ID, INT_ID);
00198 }
00199
00200
00201 this->table_[i].next_ = &this->table_[i];
00202 this->table_[i].prev_ = &this->table_[i];
00203 }
00204
00205 this->cur_size_ = 0;
00206
00207 return 0;
00208 }
00209
00210 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00211 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::bind_i (const EXT_ID &ext_id,
00212 const INT_ID &int_id,
00213 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *&entry)
00214 {
00215 size_t loc;
00216 int result = this->shared_find (ext_id, entry, loc);
00217
00218 if (result == -1)
00219 {
00220 void *ptr;
00221
00222 ACE_ALLOCATOR_RETURN (ptr,
00223 this->entry_allocator_->malloc (sizeof (ACE_Hash_Map_Entry<EXT_ID, INT_ID>)),
00224 -1);
00225
00226 entry = new (ptr) ACE_Hash_Map_Entry<EXT_ID, INT_ID> (ext_id,
00227 int_id,
00228 this->table_[loc].next_,
00229 &this->table_[loc]);
00230 this->table_[loc].next_ = entry;
00231 entry->next_->prev_ = entry;
00232 this->cur_size_++;
00233 return 0;
00234 }
00235 else
00236 return 1;
00237 }
00238
00239 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00240 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::trybind_i (const EXT_ID &ext_id,
00241 INT_ID &int_id,
00242 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *&entry)
00243 {
00244 size_t loc;
00245 int result = this->shared_find (ext_id, entry, loc);
00246
00247 if (result == -1)
00248 {
00249
00250 void *ptr;
00251 ACE_ALLOCATOR_RETURN (ptr,
00252 this->entry_allocator_->malloc (sizeof (ACE_Hash_Map_Entry<EXT_ID, INT_ID>)),
00253 -1);
00254
00255 entry = new (ptr) ACE_Hash_Map_Entry<EXT_ID, INT_ID> (ext_id,
00256 int_id,
00257 this->table_[loc].next_,
00258 &this->table_[loc]);
00259 this->table_[loc].next_ = entry;
00260 entry->next_->prev_ = entry;
00261 this->cur_size_++;
00262 return 0;
00263 }
00264 else
00265 return 1;
00266 }
00267
00268 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00269 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::unbind_i (const EXT_ID &ext_id,
00270 INT_ID &int_id)
00271 {
00272 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *temp;
00273
00274 size_t loc;
00275 int result = this->shared_find (ext_id, temp, loc);
00276
00277 if (result == -1)
00278 {
00279 errno = ENOENT;
00280 return -1;
00281 }
00282
00283 int_id = temp->int_id_;
00284
00285 return this->unbind_i (temp);
00286 }
00287
00288 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00289 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::unbind_i (ACE_Hash_Map_Entry<EXT_ID, INT_ID> *entry)
00290 {
00291 entry->next_->prev_ = entry->prev_;
00292 entry->prev_->next_ = entry->next_;
00293
00294
00295 ACE_DES_FREE_TEMPLATE2 (entry, this->entry_allocator_->free,
00296 ACE_Hash_Map_Entry, EXT_ID, INT_ID);
00297
00298 this->cur_size_--;
00299 return 0;
00300 }
00301
00302 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00303 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::shared_find (const EXT_ID &ext_id,
00304 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *&entry,
00305 size_t &loc)
00306 {
00307 loc = this->hash (ext_id) % this->total_size_;
00308
00309 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *temp = this->table_[loc].next_;
00310
00311 while (temp != &this->table_[loc] && this->equal (temp->ext_id_, ext_id) == 0)
00312 temp = temp->next_;
00313
00314 if (temp == &this->table_[loc])
00315 {
00316 errno = ENOENT;
00317 return -1;
00318 }
00319 else
00320 {
00321 entry = temp;
00322 return 0;
00323 }
00324 }
00325
00326 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00327 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::rebind_i (const EXT_ID &ext_id,
00328 const INT_ID &int_id,
00329 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *&entry)
00330 {
00331 size_t dummy;
00332 if (this->shared_find (ext_id, entry, dummy) == -1)
00333 return this->bind_i (ext_id, int_id);
00334 else
00335 {
00336 entry->ext_id_ = ext_id;
00337 entry->int_id_ = int_id;
00338 return 1;
00339 }
00340 }
00341
00342 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00343 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::rebind_i (const EXT_ID &ext_id,
00344 const INT_ID &int_id,
00345 INT_ID &old_int_id,
00346 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *&entry)
00347 {
00348 size_t dummy;
00349 if (this->shared_find (ext_id, entry, dummy) == -1)
00350 return this->bind_i (ext_id, int_id);
00351 else
00352 {
00353 old_int_id = entry->int_id_;
00354 entry->ext_id_ = ext_id;
00355 entry->int_id_ = int_id;
00356 return 1;
00357 }
00358 }
00359
00360 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00361 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::rebind_i (const EXT_ID &ext_id,
00362 const INT_ID &int_id,
00363 EXT_ID &old_ext_id,
00364 INT_ID &old_int_id,
00365 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *&entry)
00366 {
00367 size_t dummy;
00368 if (this->shared_find (ext_id, entry, dummy) == -1)
00369 return this->bind_i (ext_id, int_id);
00370 else
00371 {
00372 old_ext_id = entry->ext_id_;
00373 old_int_id = entry->int_id_;
00374 entry->ext_id_ = ext_id;
00375 entry->int_id_ = int_id;
00376 return 1;
00377 }
00378 }
00379
00380
00381
00382 ACE_ALLOC_HOOK_DEFINE(ACE_Hash_Map_Iterator_Base_Ex)
00383
00384 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> void
00385 ACE_Hash_Map_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::dump_i (void) const
00386 {
00387 ACE_TRACE ("ACE_Hash_Map_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::dump_i");
00388
00389 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00390 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("index_ = %d "), this->index_));
00391 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("next_ = %x"), this->next_));
00392 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00393 }
00394
00395 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00396 ACE_Hash_Map_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::forward_i (void)
00397 {
00398 ACE_TRACE ("ACE_Hash_Map_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::forward_i");
00399
00400 if (this->map_man_->table_ == 0)
00401 return -1;
00402
00403 else if (this->index_ == -1)
00404 {
00405 this->index_++;
00406 return this->forward_i ();
00407 }
00408 else if (this->index_ >= static_cast<ssize_t> (this->map_man_->total_size_))
00409 return 0;
00410
00411 this->next_ = this->next_->next_;
00412 if (this->next_ == &this->map_man_->table_[this->index_])
00413 {
00414 while (++this->index_ < static_cast<ssize_t> (this->map_man_->total_size_))
00415 {
00416 this->next_ = this->map_man_->table_[this->index_].next_;
00417 if (this->next_ != &this->map_man_->table_[this->index_])
00418 break;
00419 }
00420 }
00421
00422 return this->index_ < static_cast<ssize_t> (this->map_man_->total_size_);
00423 }
00424
00425 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00426 ACE_Hash_Map_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::reverse_i (void)
00427 {
00428 ACE_TRACE ("ACE_Hash_Map_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::reverse_i");
00429
00430 if (this->map_man_->table_ == 0)
00431 return -1;
00432 else if (this->index_ == static_cast<ssize_t> (this->map_man_->total_size_))
00433 {
00434 this->index_--;
00435 return this->reverse_i ();
00436 }
00437 else if (this->index_ < 0)
00438 return 0;
00439
00440 this->next_ = this->next_->prev_;
00441 if (this->next_ == &this->map_man_->table_[this->index_])
00442 {
00443 while (--this->index_ >= 0)
00444 {
00445 this->next_ = this->map_man_->table_[this->index_].prev_;
00446 if (this->next_ != &this->map_man_->table_[this->index_])
00447 break;
00448 }
00449 }
00450
00451 return this->index_ >= 0;
00452 }
00453
00454
00455
00456 ACE_ALLOC_HOOK_DEFINE(ACE_Hash_Map_Const_Iterator_Base_Ex)
00457
00458 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> void
00459 ACE_Hash_Map_Const_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::dump_i (void) const
00460 {
00461 ACE_TRACE ("ACE_Hash_Map_Const_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::dump_i");
00462
00463 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00464 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("index_ = %d "), this->index_));
00465 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("next_ = %x"), this->next_));
00466 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00467 }
00468
00469 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00470 ACE_Hash_Map_Const_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::forward_i (void)
00471 {
00472 ACE_TRACE ("ACE_Hash_Map_Const_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::forward_i");
00473
00474 if (this->map_man_->table_ == 0)
00475 return -1;
00476
00477 else if (this->index_ == -1)
00478 {
00479 this->index_++;
00480 return this->forward_i ();
00481 }
00482 else if (this->index_ >= (ssize_t) this->map_man_->total_size_)
00483 return 0;
00484
00485 this->next_ = this->next_->next_;
00486 if (this->next_ == &this->map_man_->table_[this->index_])
00487 {
00488 while (++this->index_ < (ssize_t) this->map_man_->total_size_)
00489 {
00490 this->next_ = this->map_man_->table_[this->index_].next_;
00491 if (this->next_ != &this->map_man_->table_[this->index_])
00492 break;
00493 }
00494 }
00495
00496 return this->index_ < (ssize_t) this->map_man_->total_size_;
00497 }
00498
00499 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00500 ACE_Hash_Map_Const_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::reverse_i (void)
00501 {
00502 ACE_TRACE ("ACE_Hash_Map_Const_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::reverse_i");
00503
00504 if (this->map_man_->table_ == 0)
00505 return -1;
00506 else if (this->index_ == (ssize_t) this->map_man_->total_size_)
00507 {
00508 this->index_--;
00509 return this->reverse_i ();
00510 }
00511 else if (this->index_ < 0)
00512 return 0;
00513
00514 this->next_ = this->next_->prev_;
00515 if (this->next_ == &this->map_man_->table_[this->index_])
00516 {
00517 while (--this->index_ >= 0)
00518 {
00519 this->next_ = this->map_man_->table_[this->index_].prev_;
00520 if (this->next_ != &this->map_man_->table_[this->index_])
00521 break;
00522 }
00523 }
00524
00525 return this->index_ >= 0;
00526 }
00527
00528 ACE_END_VERSIONED_NAMESPACE_DECL
00529
00530 #endif