forked from siongui/go-succinct-data-structure-trie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrozentriemap.go
93 lines (76 loc) · 2.11 KB
/
frozentriemap.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
package bits
import (
"bytes"
)
/*
FrozenTrieMap maps words in a trie onto indices.
@param data A string representing the encoded trie.
@param directoryData A string representing the RankDirectory. The global L1
and L2 constants are used to determine the L1Size and L2size.
@param nodeCount The number of nodes in the trie.
*/
type FrozenTrieMap struct {
Ft FrozenTrie
keys RankDirectory
words uint
}
func (f *FrozenTrieMap) Create(teData string, nodeCount uint) {
finalNodes := BitWriter{}
rd := CreateRankDirectory(teData, nodeCount*2+1, L1, L2)
f.Ft.Init(teData, rd.GetData(), nodeCount)
f.Ft.Apply(func(node FrozenTrieNode) {
if node.final {
finalNodes.Write(1, 1)
f.words++
} else {
finalNodes.Write(0, 1)
}
})
f.keys = CreateRankDirectory(finalNodes.GetData(), nodeCount, L1, L2)
}
func (f *FrozenTrieMap) Init(ft FrozenTrie, keys RankDirectory) {
f.Ft = ft
f.keys = keys
}
func (f *FrozenTrieMap) LookupIndex(word string) (index uint, found bool) {
node := f.Ft.GetRoot()
wordBytes := []byte(word)
for _, i := range wordBytes {
var child FrozenTrieNode
var j uint = 0
for ; j < node.GetChildCount(); j++ {
child = node.GetChild(j)
if child.letter == i {
break
}
}
if j == node.GetChildCount() {
return 0, false
}
node = child
}
return f.keys.Rank(1, node.index), node.final
}
func (f *FrozenTrieMap) ReverseLookup(keyIndex uint) (word string) {
var resultBytes []byte
trieNodeNumber := f.keys.Select(1, keyIndex)
for trieNodeNumber > 0 {
node := f.Ft.GetNodeByIndex(trieNodeNumber)
resultBytes = append([]byte{node.letter}, resultBytes...)
parentOffset := f.Ft.directory.Select(1, trieNodeNumber+1)
trieNodeNumber = f.Ft.directory.Rank(0, parentOffset) - 1
}
return string(resultBytes)
}
func (f *FrozenTrieMap) GetBuffer() []byte {
var result bytes.Buffer
result.WriteString(f.Ft.data.GetData())
result.WriteString(f.Ft.directory.GetData())
return result.Bytes()
}
func (f *FrozenTrieMap) GetOffsets() []byte {
var result bytes.Buffer
result.WriteString(f.keys.data.GetData())
result.WriteString(f.keys.directory.GetData())
return result.Bytes()
}