_M_next_resize =static_cast<std::size_t>(__builtin_ceil(*__p * _M_max_load_factor));return*__p; } 找到大于当前桶值得下一个桶值,在质数表中查找。质数表在hashtable_aux中,特别的,有一个_S_growth_factor常量,它要求下一个膨胀的桶大小至少是上一个的指定倍数,该值通常为2。
9) 扩容(resize)就是重新计算容量,向HashMap对象里不停的添加元素,而HashMap对象内部的数组无法装载更多的元素时,对象就需要扩大数组的长度,以便能装入更多的元素。
_All>:: resize(size_type __num_elements_hint) { const size_type __old_n = _M_bu...
// 伪代码示例,非实际可编译代码 void resize() { size_t new_capacity = current_capacity * 2; // 计算新容量 unordered_map<KeyType, ValueType> new_table(new_capacity); // 创建新哈希表 // 重新哈希并迁移元素 for (const auto& pair : *this) { new_table.insert(pair); } /...
_M_next_resize = __builtin_floor(__n_bkt * (long double)_M_max_load_factor); return std::make_pair(false, 0); } else return std::make_pair(false, 0); }} // namespace __detail} // namespace std 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16....
(auto ch:key){hash*=131;hash+=ch;}returnhash;}};namespace open_address{enumState{EMPTY,EXIST,DELETE};template<classK,classV>struct HashData{pair<K,V>_kv;State _state=EMPTY;};template<classK,classV,classHash=HashFunc<K>>classHashTable{public:HashTable(){_tables.resize(10);}boolInsert...
unique(const value_type& __obj) { // 首先判断buckets的大小是否需要增加 resize(_...
resize(10, nullptr); } ~HashTable() { // 依次把每个桶释放 for (size_t i = 0; i < _tables.size(); i++) { Node* cur = _tables[i]; while (cur) { Node* next = cur->_next; delete cur; cur = next; } _tables[i] = nullptr; } } pair<Iterator, bool> Insert(const T&...
HashTable()//构造函数{_tables.resize(10);//一开始先开10个大小的空间} 1. 2. 3. 4. 5、析构函数 ~HashTable()//析构函数{for(size_ti=0;i<_tables.size();i++)//遍历哈希表{Node*cur=_tables[i];Node*next=nullptr;while(cur){next=cur->next;delete cur;//释放cur=next;}_tables[i...
对_Prime_rehash_policy的探索(hashtable_c++0x.cc)我们可以发现一个数组:__prime_list。并且此时你又可以摸清其中的一部分实现:当这个map过大的时候,它就会resize其本身。当抛开这个我们先把这个质数表找到。 然后呢 打开了(hashtable-aux.cc)里面就可以看到这个数组了!