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 = 0;
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 = 0;
00216 if (this->shared_find (ext_id, entry, loc) == -1)
00217 {
00218 void *ptr = 0;
00219
00220 ACE_ALLOCATOR_RETURN (ptr,
00221 this->entry_allocator_->malloc (sizeof (ACE_Hash_Map_Entry<EXT_ID, INT_ID>)),
00222 -1);
00223
00224 entry = new (ptr) ACE_Hash_Map_Entry<EXT_ID, INT_ID> (ext_id,
00225 int_id,
00226 this->table_[loc].next_,
00227 &this->table_[loc]);
00228 this->table_[loc].next_ = entry;
00229 entry->next_->prev_ = entry;
00230 this->cur_size_++;
00231 return 0;
00232 }
00233 else
00234 return 1;
00235 }
00236
00237 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00238 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::trybind_i (const EXT_ID &ext_id,
00239 INT_ID &int_id,
00240 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *&entry)
00241 {
00242 size_t loc = 0;
00243 if (this->shared_find (ext_id, entry, loc) == -1)
00244 {
00245
00246 void *ptr = 0;
00247 ACE_ALLOCATOR_RETURN (ptr,
00248 this->entry_allocator_->malloc (sizeof (ACE_Hash_Map_Entry<EXT_ID, INT_ID>)),
00249 -1);
00250
00251 entry = new (ptr) ACE_Hash_Map_Entry<EXT_ID, INT_ID> (ext_id,
00252 int_id,
00253 this->table_[loc].next_,
00254 &this->table_[loc]);
00255 this->table_[loc].next_ = entry;
00256 entry->next_->prev_ = entry;
00257 this->cur_size_++;
00258 return 0;
00259 }
00260 else
00261 return 1;
00262 }
00263
00264 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00265 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::unbind_i (const EXT_ID &ext_id,
00266 INT_ID &int_id)
00267 {
00268 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *temp;
00269
00270 size_t loc = 0;
00271 if (this->shared_find (ext_id, temp, loc) == -1)
00272 {
00273 errno = ENOENT;
00274 return -1;
00275 }
00276
00277 int_id = temp->int_id_;
00278
00279 return this->unbind_i (temp);
00280 }
00281
00282 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00283 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)
00284 {
00285 entry->next_->prev_ = entry->prev_;
00286 entry->prev_->next_ = entry->next_;
00287
00288
00289 ACE_DES_FREE_TEMPLATE2 (entry, this->entry_allocator_->free,
00290 ACE_Hash_Map_Entry, EXT_ID, INT_ID);
00291
00292 --this->cur_size_;
00293 return 0;
00294 }
00295
00296 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00297 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::shared_find (const EXT_ID &ext_id,
00298 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *&entry,
00299 size_t &loc)
00300 {
00301 if (this->total_size_ == 0)
00302 {
00303 return -1;
00304 }
00305
00306 loc = this->hash (ext_id) % this->total_size_;
00307
00308 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *temp = this->table_[loc].next_;
00309
00310 while (temp != &this->table_[loc] && this->equal (temp->ext_id_, ext_id) == 0)
00311 temp = temp->next_;
00312
00313 if (temp == &this->table_[loc])
00314 {
00315 errno = ENOENT;
00316 return -1;
00317 }
00318 else
00319 {
00320 entry = temp;
00321 return 0;
00322 }
00323 }
00324
00325 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00326 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::rebind_i (const EXT_ID &ext_id,
00327 const INT_ID &int_id,
00328 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *&entry)
00329 {
00330 size_t dummy = 0;
00331 if (this->shared_find (ext_id, entry, dummy) == -1)
00332 return this->bind_i (ext_id, int_id);
00333 else
00334 {
00335 entry->ext_id_ = ext_id;
00336 entry->int_id_ = int_id;
00337 return 1;
00338 }
00339 }
00340
00341 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00342 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::rebind_i (const EXT_ID &ext_id,
00343 const INT_ID &int_id,
00344 INT_ID &old_int_id,
00345 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *&entry)
00346 {
00347 size_t dummy = 0;
00348 if (this->shared_find (ext_id, entry, dummy) == -1)
00349 return this->bind_i (ext_id, int_id);
00350 else
00351 {
00352 old_int_id = entry->int_id_;
00353 entry->ext_id_ = ext_id;
00354 entry->int_id_ = int_id;
00355 return 1;
00356 }
00357 }
00358
00359 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00360 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::rebind_i (const EXT_ID &ext_id,
00361 const INT_ID &int_id,
00362 EXT_ID &old_ext_id,
00363 INT_ID &old_int_id,
00364 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *&entry)
00365 {
00366 size_t dummy = 0;
00367 if (this->shared_find (ext_id, entry, dummy) == -1)
00368 return this->bind_i (ext_id, int_id);
00369 else
00370 {
00371 old_ext_id = entry->ext_id_;
00372 old_int_id = entry->int_id_;
00373 entry->ext_id_ = ext_id;
00374 entry->int_id_ = int_id;
00375 return 1;
00376 }
00377 }
00378
00379
00380
00381 ACE_ALLOC_HOOK_DEFINE(ACE_Hash_Map_Iterator_Base_Ex)
00382
00383 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> void
00384 ACE_Hash_Map_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::dump_i (void) const
00385 {
00386 ACE_TRACE ("ACE_Hash_Map_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::dump_i");
00387
00388 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00389 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("index_ = %d "), this->index_));
00390 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("next_ = %x"), this->next_));
00391 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00392 }
00393
00394 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00395 ACE_Hash_Map_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::forward_i (void)
00396 {
00397 ACE_TRACE ("ACE_Hash_Map_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::forward_i");
00398
00399 if (this->map_man_->table_ == 0)
00400 return -1;
00401
00402 else if (this->index_ == -1)
00403 {
00404 this->index_++;
00405 return this->forward_i ();
00406 }
00407 else if (this->index_ >= static_cast<ssize_t> (this->map_man_->total_size_))
00408 return 0;
00409
00410 this->next_ = this->next_->next_;
00411 if (this->next_ == &this->map_man_->table_[this->index_])
00412 {
00413 while (++this->index_ < static_cast<ssize_t> (this->map_man_->total_size_))
00414 {
00415 this->next_ = this->map_man_->table_[this->index_].next_;
00416 if (this->next_ != &this->map_man_->table_[this->index_])
00417 break;
00418 }
00419 }
00420
00421 return this->index_ < static_cast<ssize_t> (this->map_man_->total_size_);
00422 }
00423
00424 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00425 ACE_Hash_Map_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::reverse_i (void)
00426 {
00427 ACE_TRACE ("ACE_Hash_Map_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::reverse_i");
00428
00429 if (this->map_man_->table_ == 0)
00430 return -1;
00431 else if (this->index_ == static_cast<ssize_t> (this->map_man_->total_size_))
00432 {
00433 --this->index_;
00434 return this->reverse_i ();
00435 }
00436 else if (this->index_ < 0)
00437 return 0;
00438
00439 this->next_ = this->next_->prev_;
00440 if (this->next_ == &this->map_man_->table_[this->index_])
00441 {
00442 while (--this->index_ >= 0)
00443 {
00444 this->next_ = this->map_man_->table_[this->index_].prev_;
00445 if (this->next_ != &this->map_man_->table_[this->index_])
00446 break;
00447 }
00448 }
00449
00450 return this->index_ >= 0;
00451 }
00452
00453
00454
00455 ACE_ALLOC_HOOK_DEFINE(ACE_Hash_Map_Const_Iterator_Base_Ex)
00456
00457 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> void
00458 ACE_Hash_Map_Const_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::dump_i (void) const
00459 {
00460 ACE_TRACE ("ACE_Hash_Map_Const_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::dump_i");
00461
00462 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00463 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("index_ = %d "), this->index_));
00464 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("next_ = %x"), this->next_));
00465 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00466 }
00467
00468 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00469 ACE_Hash_Map_Const_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::forward_i (void)
00470 {
00471 ACE_TRACE ("ACE_Hash_Map_Const_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::forward_i");
00472
00473 if (this->map_man_->table_ == 0)
00474 return -1;
00475
00476 else if (this->index_ == -1)
00477 {
00478 ++this->index_;
00479 return this->forward_i ();
00480 }
00481 else if (this->index_ >= (ssize_t) this->map_man_->total_size_)
00482 return 0;
00483
00484 this->next_ = this->next_->next_;
00485 if (this->next_ == &this->map_man_->table_[this->index_])
00486 {
00487 while (++this->index_ < (ssize_t) this->map_man_->total_size_)
00488 {
00489 this->next_ = this->map_man_->table_[this->index_].next_;
00490 if (this->next_ != &this->map_man_->table_[this->index_])
00491 break;
00492 }
00493 }
00494
00495 return this->index_ < (ssize_t) this->map_man_->total_size_;
00496 }
00497
00498 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00499 ACE_Hash_Map_Const_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::reverse_i (void)
00500 {
00501 ACE_TRACE ("ACE_Hash_Map_Const_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::reverse_i");
00502
00503 if (this->map_man_->table_ == 0)
00504 return -1;
00505 else if (this->index_ == (ssize_t) this->map_man_->total_size_)
00506 {
00507 --this->index_;
00508 return this->reverse_i ();
00509 }
00510 else if (this->index_ < 0)
00511 return 0;
00512
00513 this->next_ = this->next_->prev_;
00514 if (this->next_ == &this->map_man_->table_[this->index_])
00515 {
00516 while (--this->index_ >= 0)
00517 {
00518 this->next_ = this->map_man_->table_[this->index_].prev_;
00519 if (this->next_ != &this->map_man_->table_[this->index_])
00520 break;
00521 }
00522 }
00523
00524 return this->index_ >= 0;
00525 }
00526
00527 ACE_END_VERSIONED_NAMESPACE_DECL
00528
00529 #endif