|  | 
 
 发表于 2008-11-21 21:12:59
|
显示全部楼层 
| 源码如下 {
 size_t loc;
 int result = this->shared_find (ext_id, entry, loc);
 
 if (result == -1)
 {
 // Not found. 如果该绑定不存在 hashtable时,进行插入。
 void *ptr;
 ACE_ALLOCATOR_RETURN (ptr,
 this->entry_allocator_->malloc (sizeof (ACE_Hash_Map_Entry<EXT_ID, INT_ID>)),
 -1);
 
 entry = new (ptr) ACE_Hash_Map_Entry<EXT_ID, INT_ID> (ext_id,
 int_id,
 this->table_[loc].next_,
 &this->table_[loc]);
 this->table_[loc].next_ = entry;  // 在这里我们可以看到是以列表的形式插入到loc位置的。loc就是EXT_ID的hash值,不同的EXT_ID hash值可能是一样的。在hash值一样时,把他们通过列表链接在一起。我们可以看一下shared_find  函数什么时候返回-1.
 entry->next_->prev_ = entry;
 this->cur_size_++;
 return 0;
 }
 else
 return 1;
 }
 
 
 
 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::shared_find (const EXT_ID &ext_id,
 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *&entry,
 size_t &loc)
 {
 loc = this->hash (ext_id) % this->total_size_; // 根据hash值,计算存储的位置。如果open把size设置为1,这个hash map就变成了链表了。
 
 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *temp = this->table_[loc].next_;
 
 while (temp != &this->table_[loc] && this->equal (temp->ext_id_, ext_id) == 0) // 在loc位置的link列表查找自己。
 temp = temp->next_;
 
 if (temp == &this->table_[loc])
 { // 找不到时。。
 
 errno = ENOENT;
 return -1;
 }
 else
 {
 entry = temp;
 return 0;
 }
 }
 | 
 |