-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhashtable.go
More file actions
78 lines (61 loc) · 1.5 KB
/
hashtable.go
File metadata and controls
78 lines (61 loc) · 1.5 KB
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
/*
* Implementation of a Hash Table which stores containers of
* either RED_BLACK_TREE or Slice
* 14/5/18
* Author Callan Taylor
*/
package main
import "fmt"
type htable struct {
keys []*container
frequencies []int
numKeys int
capacity int
}
func newHtable(size int, t string) *htable {
keyArray := make([]*container, size)
frequenciesArray := make([]int, size)
var numkeys int = 0
var capacity int = size
for i := 0; i < size; i++ {
frequenciesArray[i] = 0
keyArray[i] = newContainer(t)
}
newHtable := htable{keys:keyArray, frequencies:frequenciesArray,
numKeys:numkeys, capacity:capacity}
return &newHtable
}
func htableWordToInt(word string) uint {
var result uint
for i := 0; i < len(word); i++ {
result = 31 * result + uint(word[i])
}
return result
}
func htableInsertWord(h *htable, word string) {
position := htableWordToInt(word) % uint(h.capacity)
if h.keys[position].containerType == "" {
containerAdd(h.keys[position], word)
h.frequencies[position] = 1
h.numKeys++
return
} else {
containerAdd(h.keys[position], word)
h.frequencies[position]++
}
}
func htableSearch(h *htable, word string) bool {
position := htableWordToInt(word) % uint(h.capacity)
if h.keys[position].containerType != "" {
return containerSearch(h.keys[position], word)
}
return false
}
func htablePrint(h *htable) {
for i := 0; i < h.capacity; i++ {
if h.frequencies[i] != 0 {
containerPrint(h.keys[i])
}
fmt.Println()
}
}