// 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));
귀여움 ?
이번엔 콜리전 해결함
콜리전은 어쩔건데
후다닥 추가중
추가했다
아재밌어
해시맵 구현에 해시맵을 쓰누
테이블 {} 대신에 배열로 바꾸고 삽입, 참조할 때 table[hashingKey % tableSize] ㄱㄱ
오~ 맞네 수정했으