// hashamp



class HashNode {
#_key;
#_value;
#_next;
constructor(key, value) {
this.#_key = key;
this.#_value = value;
this.#_next = null;
}
get value() {
return this.#_value;
}
get key() {
return this.#_key;
}
set next(node) {
this.#_next = node;
}
get next() {
return this.#_next;
}
}

function Hash(tableSize) {
const size = tableSize;
return function(key) {
return key % size;
}
}

class HashMap {
#_size;
#_table;
#_hash;
constructor(tableSize) {
this.#_size = tableSize;
this.#_table = {};
this.#_hash = Hash(this.#_size);
}
insert(key, value) {
const node = new HashNode(key, value);
const hashingKey = this.#_hash(key);
if(this.#_table[hashingKey]) {
let current = this.#_table[hashingKey];
while(current.next) {
console.log(current.value);
current = current.next;
}
current.next = node;
} else {
this.#_table[hashingKey] = node;
}
}
search(key) {
const hashingKey = this.#_hash(key);
let current = this.#_table[hashingKey];
while(current) {
if(current.key === key) {
break;
} else {
current = current.next;
}
}
if(current) {
return current.value;
} else {
return null;
}
}
}

const map = new HashMap(200);
map.insert(100, 100);
map.insert(300, 300);
map.insert(500, 500);

console.log(map.search(300));




귀여움 ?


이번엔 콜리전 해결함