-
Notifications
You must be signed in to change notification settings - Fork 2
/
hashTable.ts
125 lines (104 loc) · 2.8 KB
/
hashTable.ts
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { HashTableApi, DataType } from "./helper.d.ts";
import { hashFunction } from "./hashFunction.ts";
import { HashNode } from "./hashNode.ts";
import {
addGenerator,
removeGenerator,
updateGenerator,
getGenerator,
iteratorGenerator
} from "./generators.ts";
export class HashTable implements HashTableApi {
#table: Array<any>
public length: number
constructor() {
this.#table = new Array()
this.length = 0
}
add(data: DataType<any>) {
const index = hashFunction(data.key)
const node = new HashNode(data)
if (this.#table[index] === undefined) {
this.#table[index] = node
this.length++
return true
}
if (this.#table[index].next === null) {
this.#table[index].next = node
return true
}
let currentNode = this.#table[index].next
// generator function that returns an iterator
const iterator = addGenerator(currentNode, node)
const iteratorNext = iterator.next()
if (iteratorNext.value) {
return true
}
return false;
}
remove(key: string) {
let index = hashFunction(key)
if (this.#table[index] === undefined) return false
if (this.#table[index].data.key === key) {
delete this.#table[index]
this.length--
return true;
}
if (this.#table[index].next !== null) {
let currentNode = this.#table[index]
// generator function that returns an iterator
const iterator = removeGenerator(key, currentNode)
const iteratorNext = iterator.next()
if (iteratorNext.value) {
return true
}
}
return false
}
update(key: string, newValue: any) {
let index = hashFunction(key)
if (this.#table[index] === undefined) return false
if (this.#table[index].data.key === key) {
this.#table[index].data.value = newValue
return this.#table[index].data.value;
}
if (this.#table[index].next !== null) {
let currentNode = this.#table[index]
// generator function that returns an iterator
const iterator = updateGenerator(key, newValue, currentNode)
const iteratorNext = iterator.next()
if (iteratorNext.value) {
return true
}
}
return false
}
get(key: string) {
let index = hashFunction(key)
if (this.#table[index] === undefined) return false
if (this.#table[index].data.key === key) {
return this.#table[index].data;
}
if (this.#table[index].next !== null) {
let currentNode = this.#table[index]
// generator function that returns an iterator
const iterator = getGenerator(key, currentNode)
const iteratorNext = iterator.next()
if (iteratorNext.value) {
return iteratorNext.value
}
}
return false
}
log(column: string[] = []) {
if (column.length > 0) {
return console.table(this.#table, column)
}
return console.table(this.#table);
}
iterator() {
// generator function that returns an iterator
const iterator = iteratorGenerator(this.#table)
return iterator
}
}