-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashtable.go
168 lines (155 loc) · 2.68 KB
/
hashtable.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
/*
Package hashtable provides a Hash Table
*/
package antlr
import (
// "math"
"fmt"
)
const arrayLength = 1000
// HashTable : a hash table data structure
type HashTable struct {
data [arrayLength]*LinkedList
count int
}
type listData struct {
key *DFAState
value *DFAState
}
// New : a hash table constructor
func NewHashTable() *HashTable {
return &HashTable{
[arrayLength]*LinkedList{},
0,
}
}
func (h *HashTable) MyIterator() <-chan *DFAState {
c := make(chan *DFAState)
go func() {
defer close(c)
for x := range h.data {
if h.data[x] == nil {
continue
}
node := h.data[x].Head
for {
if node != nil {
d := node.Data.(listData)
c <- d.key
break
} else {
break
}
node = node.Next
}
}
}()
return c
}
func hash(s *DFAState) int {
// h := 0
// char[0] * 31^n-1 + char[1] * 31^n-2 + ... + char[n-1]
// for pos, char := range s {
// h += int(char) * int(math.Pow(31, float64(len(s)-pos+1)))
// }
if false {
fmt.Print("hash ")
fmt.Print(s)
fmt.Print(" => ")
}
hash := s.hash()
if false {
fmt.Println(hash)
}
return hash
}
func equals(one *DFAState, other *DFAState) bool {
if false {
fmt.Print("equals ")
fmt.Print(one)
fmt.Print(" ")
fmt.Print(other)
fmt.Print(" => ")
}
result := one.equals(other)
if false {
fmt.Println(result)
}
return result
}
func index(hash int) int {
return hash % arrayLength
}
// Set : set a key and value
func (h *HashTable) Set(k *DFAState, v *DFAState) *HashTable {
if false {
fmt.Print("Set ")
fmt.Print(k)
fmt.Print(" ")
fmt.Println(v)
}
index := index(hash(k))
if false {
fmt.Print("index ")
fmt.Println(index)
}
if h.data[index] == nil {
h.data[index] = NewLinkedList()
h.data[index].Append(listData{k, v})
h.count++
} else {
node := h.data[index].Head
for {
if node != nil {
d := node.Data.(listData)
if equals(k, d.key) {
d.value = v
if false {
fmt.Print("Entered ")
fmt.Print(d.key)
fmt.Print(" ")
fmt.Println(v)
}
break
}
} else {
h.data[index].Append(listData{k, v})
h.count++
break
}
node = node.Next
}
}
return h
}
func (h *HashTable) Len() int {
return h.count
}
// Get : get a value by key
func (h *HashTable) Get(k *DFAState) (result *DFAState, ok bool) {
if false {
fmt.Print("Get ")
fmt.Println(k)
}
index := index(hash(k))
if false {
fmt.Print("index ")
fmt.Println(index)
}
linkedList := h.data[index]
if linkedList == nil {
return nil, false
}
node := linkedList.Head
for {
if node != nil {
d := node.Data.(listData)
if equals(d.key, k) {
return d.value, true
}
} else {
return nil, false
}
node = node.Next
}
}