File size: 1,317 Bytes
e4bf523
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict'
/**

 * 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;