Spaces:
Sleeping
Sleeping
| /** | |
| * Copyright (c) 2017~2019, OBCon Inc. | |
| * All rights reserved. | |
| */ | |
| /** | |
| * @file | |
| * @copyright 2017~2019, OBCon Inc. | |
| * @author gye hyun james kim [pnuskgh@gmail.com] | |
| */ | |
| class HashTable { | |
| constructor(size) { | |
| this.size = size; //--- HashTable의 크기 | |
| this.keys = new Array(size); | |
| this.values = new Array(size); | |
| this.limit = 0; | |
| } | |
| put(key, value) { | |
| if (this.size <= this.limit) { | |
| throw 'Error : HashTable is full.'; | |
| } | |
| let idx = this._hash(key); | |
| while ((this.keys[idx]) && (this.keys[idx] != key)) { | |
| idx = (idx + 1) % this.size; | |
| } | |
| this.keys[idx] = key; | |
| this.values[idx] = value; | |
| this.limit = this.limit + 1; | |
| } | |
| get(key) { | |
| let idx = this._hash(key); | |
| while (this.keys[idx] != key) { | |
| idx = (idx + 1) % this.size; | |
| } | |
| return this.values[idx]; | |
| } | |
| _hash(key) { //--- Hash 함수 (key로 인덱스를 생성 한다.) | |
| if (!Number.isInteger(key)) { | |
| throw 'Error : key is not integer.'; | |
| } | |
| return key % this.size; | |
| } | |
| } | |
| module.exports = HashTable; | |