Published
Edited
Jul 11, 2022
Insert cell
Insert cell
Insert cell
Insert cell
class DoubleHashing extends SeparateChainingHash {
constructor(...args) {
super(...args);
}

resetHash() {
for (let i = 0; i < this.length; i++) { this[i] = undefined; }
}

find(key) {
let i = h(key.charCodeAt(), this.length);
while(this[i] !== undefined && this[i] !== key) {
i += g(key.charCodeAt(), this.length);
i = i % this.length;
}
return i;
}

insert(key, rehash) {
if (key !== "Deleted") {
let i = this.find(key);
if (this[i] === key) {
throw `This key already exists: ${key}`
};
this[i] = key;
if (!rehash) this.keys.push(key);

let lamb = this.keys.length / this.length;
if (lamb > this.lambMax) this.rehash();
}
}

remove(key) {
let i = this.find(key);
if (this[i] === undefined) {
throw `This key does not exist: ${key}`
};

this[i] = "Deleted";
this.keys = this.keys.filter(item => item !== key);
this.keys.push("Deleted");

let lamb = this.keys.length / this.length;
if (lamb < this.lambMin) this.rehash();
}
}
Insert cell
Insert cell
{
let dh = new DoubleHashing(10, h, 1/4, 3/4);
["a", "b", "c", "d", "e", "f", "g"].forEach((key) => {
dh.insert(key);
});
return dh;
}
Insert cell
Insert cell
{
let dh = new DoubleHashing(10, h, 1/4, 3/4);
["a", "b", "c", "d", "e", "f", "g"].forEach((key) => {
dh.insert(key);
});
dh.insert("z");
return dh;
}
Insert cell
Insert cell
{
let dh = new DoubleHashing(10, h, 1/4, 3/4);
["a", "b", "c", "d", "f", "g"].forEach((key) => {
dh.insert(key);
});
dh.remove("c");
dh.insert("c");
return dh;
}
Insert cell
Insert cell
{
let dh = new DoubleHashing(10, h, 1/4, 3/4);
["a", "b", "c", "d", "e"].forEach((key) => {
dh.insert(key);
});
dh.remove("a");
dh.insert("a");
dh.remove("a");
dh.insert("a");
dh.remove("a");
dh.insert("a");
return dh;
}
Insert cell
Insert cell
{
let arr = [];
let c = h("a".charCodeAt(), 10);
arr.push(c);
for (let i = 0; i < 3; i++){
c += g("a".charCodeAt(), 10);
arr.push(c % 10);
}

let c2 = h("a".charCodeAt(), 16);
for (let i = 0; i < 3; i++){
c2 += g("a".charCodeAt(), 16);
arr.push(c2 % 16);
}
return arr;
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more