-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdawgnode.go
182 lines (152 loc) · 3.27 KB
/
dawgnode.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
174
175
176
177
178
179
180
181
182
package graph
import (
"sort"
"strconv"
)
type dawgNode interface {
getID() int
setID(int)
isFinal() bool
setFinal(bool)
getEdges() map[string]dawgNode
setEdges(map[string]dawgNode)
setEdge(string, dawgNode)
setPayload(p NodePayload)
getPayload() NodePayload
String() string
hash() int32
equals(dawgNode) bool
}
type intermediaryNode struct {
id int
final bool
edges map[string]dawgNode
}
type finalNode struct {
*intermediaryNode
payload NodePayload
}
// NodePayload - Interface used to store custom data in the graph, linked to a token
type NodePayload interface {
Encode() []byte
Decode([]byte) (NodePayload, error)
String() string
}
/// DummyPayload -- use this struct to inherit default interface implementations
type DummyPayload struct {
}
/// implement dummy payload interface functions
func (d *DummyPayload) Encode() []byte {
return nil
}
func (d *DummyPayload) String() string {
return ""
}
func (d *DummyPayload) Decode(enc []byte) (NodePayload, error) {
return NodePayload(nil), nil
}
/// implement node interface methods
func (i *intermediaryNode) setPayload(p NodePayload) {
return
}
func (i *intermediaryNode) getPayload() NodePayload {
return nil
}
func (i *intermediaryNode) getID() int {
return i.id
}
func (i *intermediaryNode) setID(id int) {
i.id = id
}
func (i *intermediaryNode) isFinal() bool {
return i.final
}
func (i *intermediaryNode) setFinal(f bool) {
i.final = f
}
func (i *intermediaryNode) getEdges() map[string]dawgNode {
return i.edges
}
func (i *intermediaryNode) setEdges(m map[string]dawgNode) {
i.edges = m
}
func (i *intermediaryNode) setEdge(k string, v dawgNode) {
i.edges[k] = v
}
func (i *intermediaryNode) String() (s string) {
if i.isFinal() {
s += "1"
} else {
s += "0"
}
temp := make([]string, len(i.getEdges()))
j := 0
for k := range i.getEdges() {
temp[j] = k
j++
}
sort.Strings(temp)
for _, k := range temp {
if i.getEdges()[k] == nil {
continue
}
s += "_" + k
s += "_" + strconv.Itoa(i.getEdges()[k].getID())
}
return s
}
func (f *finalNode) String() (s string) {
s += f.intermediaryNode.String()
if f.payload != nil {
s += f.payload.String()
}
return
}
func (f *finalNode) setPayload(p NodePayload) {
f.payload = p
}
func (f *finalNode) getPayload() NodePayload {
return f.payload
}
func (i *intermediaryNode) hash() (h int32) {
return int32(calcHash([]byte(i.String())))
}
func (f *finalNode) hash() (h int32) {
return int32(calcHash([]byte(f.String())))
}
func (i *intermediaryNode) equals(j dawgNode) (ret bool) {
ret = false
if i.String() == j.String() {
ret = true
}
return
}
func (f *finalNode) equals(j dawgNode) (ret bool) {
ret = false
if f.String() == j.String() {
ret = true
}
return
}
func newDawgNode(isFinal bool) dawgNode {
NextId++
i := &intermediaryNode{NextId, isFinal, make(map[string]dawgNode)}
// log("creating node [%v] - [%p][%s]\n", isFinal, i, i)
if !isFinal {
return i
}
z := &finalNode{i, nil}
// log("creating node [%v] - [%p][%s]\n", isFinal, z.intermediaryNode, z)
return z
}
func newDawgNodeManual(id int, isFinal bool) dawgNode {
i := &intermediaryNode{id, isFinal, make(map[string]dawgNode)}
if !isFinal {
return i
}
return &finalNode{i, nil}
}
type uncheckedDawgNode struct {
parent, child dawgNode
symbol string
}