-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
173 lines (157 loc) · 4.22 KB
/
main.go
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import "fmt"
var (
minLoadFactor = 0.25
maxLoadFactor = 0.75
defaultTableSize = 3
)
type Record struct {
key int
value int
next *Record
}
type Hash struct {
records []*Record
}
type HashTable struct {
table *Hash
nRecords *int
}
// createHashTable: Called by checkLoadFactorAndUpdate when creating a new hash, for internal use only.
func createHashTable(tableSize int) HashTable {
num := 0
hash := Hash{make([]*Record, tableSize)}
return HashTable{table: &hash, nRecords:&num}
}
// CreateHashTable: Called by the user to create a hashtable.
func CreateHashTable() HashTable {
num := 0
hash := Hash{make([]*Record, defaultTableSize)}
return HashTable{table: &hash, nRecords:&num}
}
// hashFunction: Used to calculate the index of record within the slice
func hashFunction(key int, size int) (int) {
return key%size
}
// Display: Print the hashtable in a legible format (publicly callable)
func (h *HashTable) Display() {
fmt.Printf("----------%d elements-------\n", *h.nRecords)
for i, node := range h.table.records {
fmt.Printf("%d :", i)
for node != nil {
fmt.Printf("[%d, %d]->", node.key, node.value)
node = node.next
}
fmt.Println("nil")
}
}
// put: inserts a key into the hash table, for internal use only
func (h *HashTable) put(key int, value int) (bool){
index := hashFunction(key, len(h.table.records))
iterator := h.table.records[index]
node := Record{key, value, nil}
if iterator == nil {
h.table.records[index] = &node
} else {
prev := &Record{0, 0, nil}
for iterator != nil {
if iterator.key == key { // Key already exists
iterator.value = value
return false
}
prev = iterator
iterator = iterator.next
}
prev.next = &node
}
*h.nRecords += 1
return true
}
// Put: inserts a key into the hash table (publicly callable)
func (h *HashTable) Put(key int, value int) {
sizeChanged := h.put(key, value)
if sizeChanged == true {
h.checkLoadFactorAndUpdate()
}
}
// Get: Retrieve a value for a key from the hash table (publicly callable)
func (h *HashTable) Get(key int) (bool, int) {
index := hashFunction(key, len(h.table.records))
iterator := h.table.records[index]
for iterator != nil {
if iterator.key == key { // Key already exists
return true, iterator.value
}
iterator = iterator.next
}
return false, 0
}
// del: remove a key-value record from the hash table, for internal use only
func (h *HashTable) del(key int) (bool) {
index := hashFunction(key, len(h.table.records))
iterator := h.table.records[index]
if iterator == nil {
return false
}
if iterator.key == key {
h.table.records[index] = iterator.next
*h.nRecords--
return true
} else {
prev := iterator
iterator = iterator.next
for iterator != nil {
if iterator.key == key {
prev.next = iterator.next
*h.nRecords--
return true
}
prev = iterator
iterator = iterator.next
}
return false
}
}
// Del: remove a key-value record from the hash table (publicly available)
func (h *HashTable) Del(key int) (bool) {
sizeChanged := h.del(key)
if sizeChanged == true {
h.checkLoadFactorAndUpdate()
}
return sizeChanged
}
// getLoadFactor: calculate the loadfactor for the hashtable
// Calculated as: number of records stored / length of underlying slice used
func (h *HashTable) getLoadFactor() (float64) {
return float64(*h.nRecords)/float64(len(h.table.records))
}
// checkLoadFactorAndUpdate: if 0.25 > loadfactor or 0.75 < loadfactor,
// update the underlying slice to have have loadfactor close to 0.5
func (h *HashTable) checkLoadFactorAndUpdate() {
if *h.nRecords == 0 {
return
} else {
loadFactor := h.getLoadFactor()
if loadFactor < minLoadFactor {
fmt.Println("** Loadfactor below limit, reducing hashtable size **")
hash := createHashTable(len(h.table.records)/2)
for _, record := range h.table.records {
for record != nil {
hash.put(record.key, record.value)
record = record.next
}
}
h.table = hash.table
} else if loadFactor > maxLoadFactor {
fmt.Println("** Loadfactor above limit, increasing hashtable size **")
hash := createHashTable(*h.nRecords*2)
for _, record := range h.table.records {
for record != nil {
hash.put(record.key, record.value)
record = record.next
}
}
h.table = hash.table
}
}
}