什么是可扩展哈希?
Extendible Hashing is a dynamic hashing method wherein directories, and buckets are used to hash data. It is an aggressively flexible method in which the hash function also experiences dynamic changes. ——Extendible Hashing (Dynamic approach to DBMS)
主要特点:
Directories: The directories store addresses of the buckets in pointers. An id is assigned to each directory which may change each time when Directory Expansion takes place.
Buckets: The buckets are used to hash the actual data.
这里以cmu15-445 2022fall p1 buffer_pool为基础讲解,以下为涉及到的成员变量。
下面开始进行插入元素函数的介绍,也仅仅介绍这一个函数。
下面以插入这几个数为例,其中元素仅仅声明了key,并未标注对应的value,因为是根据key进行插入,以及查找等操作的,在下面的示例中, 你也可以理解为key == value。
发生溢出,判断global_ depth _ 与发生溢出的桶的depth_大小关系。如下图所示,二者相等,依次进行->目录扩张->桶分裂->重新连接
再次尝试插入6,还是溢出,再进行判断。
又尝试插入6,此时完成插入。
相关内容补充
发生溢出
再次尝试插入24
完成插入,后面越插入越复杂了,所以就到此为止了,注意: 不同的初始条件,插入元素得到的结果不同。
详见上方插入过程中,有一段红字标注。
代码示例
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
class A{
public:
A(){
cout <<"structure"<<endl;
m_d = 999;
}
~A(){
cout <<"distructure"<<endl;
}
int m_d;
};
int main(void){
vector<A>v1;
vector<A>v2;
// 给一个比当前元素数量大的数
v1.resize(2);// 调用A默认构造,插入v1尾部
cout << "v1 capacity: "<<v1.capacity()<<endl; // 2
cout << "v1 size: "<<v1.size();// 2
cout <<endl;
v1[0].m_d = 0;
v1[1].m_d = 1;
v2.reserve(2);
cout << "v2 capacity: "<<v2.capacity()<<endl;// 2
cout << "v2 size: "<<v2.size();// 2
cout <<endl;
// 给一个比当前元素数量小的数,测试是否能删除调用析构函数,
v2.push_back(A());// 给v2两个
v2.push_back(A());
cout <<"test distructure"<<endl;
v1.resize(1);
v2.reserve(1);// reserve一个比当前小的值,容量不会变化
cout << "v1 capacity: "<<v1.capacity()<<endl; // 2
cout << "v1 size: "<<v1.size();// 1
cout <<endl;
cout << "v2 capacity: "<<v2.capacity()<<endl;// 2
cout << "v2 size: "<<v2.size();// 2
cout <<endl;
cout << v1[1].m_d<< endl;// vector的[]并没有越界访问判断
system("pause");
return 0;
}