forked from dghubble/trie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie_storage.go
87 lines (77 loc) · 2.05 KB
/
trie_storage.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
package trie
import (
"encoding/gob"
"fmt"
"io"
)
type trieStorage struct {
// not storable on disk
// Segmenter StringSegmenter
Value interface{}
Children map[string]*trieStorage
}
// Save stores the trie to file in a gob format.
// does not store the Segmenter function
func (trie *PathTrie) Save(out io.Writer) error {
ts := trie.toTrieStorage()
return gob.NewEncoder(out).Encode(&ts)
}
// Save stores a rune-trie
func (trie *RuneTrie) Save(out io.Writer) error {
ts := trie.toTrieStorage()
return gob.NewEncoder(out).Encode(ts)
}
// LoadPathTrie reads a PathTrie from file (or io.Reader)
func LoadPathTrie(in io.Reader) (*PathTrie, error) {
var ts trieStorage
err := gob.NewDecoder(in).Decode(&ts)
if err != nil {
return nil, err
}
return ts.toPathTrie(), nil
}
// LoadRuneTrie reads a RuneTrie from file (or any io.Reader)
func LoadRuneTrie(in io.Reader) (*RuneTrie, error) {
var ts trieStorage
err := gob.NewDecoder(in).Decode(&ts)
if err != nil {
return nil, err
}
return ts.toRuneTrie()
}
func (ts *trieStorage) toPathTrie() *PathTrie {
trie := &PathTrie{segmenter: PathSegmenter, value: ts.Value, children: make(map[string]*PathTrie)}
for k, v := range ts.Children {
trie.children[k] = v.toPathTrie()
}
return trie
}
func (ts *trieStorage) toRuneTrie() (*RuneTrie, error) {
trie := &RuneTrie{value: ts.Value, children: make(map[rune]*RuneTrie)}
for k, v := range ts.Children {
rs := []rune(k)
if len(rs) != 1 {
return nil, fmt.Errorf("not a rune trie")
}
child, err := v.toRuneTrie()
if err != nil {
return nil, err
}
trie.children[rs[0]] = child
}
return trie, nil
}
func (trie *PathTrie) toTrieStorage() *trieStorage {
ts := &trieStorage{Value: trie.value, Children: make(map[string]*trieStorage)}
for k, v := range trie.children {
ts.Children[k] = v.toTrieStorage()
}
return ts
}
func (trie *RuneTrie) toTrieStorage() *trieStorage {
ts := &trieStorage{Value: trie.value, Children: make(map[string]*trieStorage)}
for k, v := range trie.children {
ts.Children[string(k)] = v.toTrieStorage()
}
return ts
}