java - 在制作自己的可扩展散列项目时,如何让目录“指向”正确的存储桶?
问题描述
我已经阅读了有关创建可扩展哈希表Extendable Hashing的 wiki 页面。现在我正在尝试将其实现到一个程序中以更好地理解它,但我无法弄清楚如何将目录实际指向存储桶。
目录类:
int GlobalDepth = 1;
int directories[];
public Directory() {
int temp = (int)Math.pow(2, GlobalDepth);
loadDirectories(temp);
}
public void loadDirectories(int n) {
for (int i = 0; i < n; i ++) {
String BinaryKey = Integer.toBinaryString(i);
String tempKey = BinaryKey.substring(0, this.GlobalDepth); // LSB
int depthKey = Integer.parseUnsignedInt(tempKey);
this.directories[i] = depthKey;
}
}
public void increaseGlobalDepth() {
this.GlobalDepth ++;
loadDirectories((int)(Math.pow(2, this.GlobalDepth))); // This method might throw an error because i think im changing the array size illegally and should instead create a temp array and copy/equal that array to this.directories
}
桶类:
private SomeObject[] item; // Class I'm using to hold all the information of something
private int Key, MaxBucketsize, LocalDepth = 1;
//Bucket next;
public Bucket() {
}
public Bucket(int BucketSize) {
this.item = new SomeObject[BucketSize]; // Initalises the number of items held in the bucket
this.MaxBucketsize = BucketSize; // Stores the max number of items that can be held in the bucket
}
public SomeObject[] getObjects() {
return this.item;
}
public boolean addObjects(int key, SomeObject item) {
boolean inserted = false;
for (int i = 0; i < this.MaxBucketsize; i ++) {
if (this.item[i] == null) { // only inserts if there is an empty spot in the bucket
this.item[i] = item;
this.item[i].setKey(key);
inserted = true;
break;
}
}
return inserted;
}
完成所有这些之后,我不确定现在如何将 2 链接在一起,就像在 wiki 页面中一样。
解决方案
推荐阅读
- .net - 为什么 VBA 的 VarType 函数说这个 COM 对象是一个字符串?(对象是 .NET 的 System.Object 类的 COM 版本的实例。)这是一个错误吗?
- python - pandas - 将两个数据框添加在一起,其中值相同
- typescript - 当父键是通用的时限制子接口的有效键
- qt - 无法打开源文件没有这样的文件或目录
- php - 根据任意开始日获取星期几的名称(不总是星期日)
- c++ - 与所有者一起分配数组的正确方法
- swift - 将 NSbutton 连接到从 Xib 加载 NSView 的方法的代码
- javascript - 如何通过 Puppeteer 获取元素的子元素
- c# - 我找不到如何将数据模型属性限制为“S”或“P”
- javascript - 如何找出文章中的列数?