Skip to content

Commit a686545

Browse files
committed
resolve claims from govet and golint
1 parent 4c4c696 commit a686545

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/tags
2+
/tmp/

ternary.go

+25-6
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
package trie
22

3+
// TernaryTrie provides ternary trie-tree.
34
type TernaryTrie struct {
45
root TernaryNode
56
}
67

8+
// NewTernaryTrie creates a ternary trie-tree.
79
func NewTernaryTrie() *TernaryTrie {
810
return &TernaryTrie{}
911
}
1012

13+
// Root returns the root node of the trie-tree.
1114
func (t *TernaryTrie) Root() Node {
1215
return &t.root
1316
}
1417

18+
// Get returns a node for key k.
1519
func (t *TernaryTrie) Get(k string) Node {
1620
return Get(t, k)
1721
}
1822

23+
// Put puts a pair of key and value to trie-tree.
1924
func (t *TernaryTrie) Put(k string, v interface{}) Node {
2025
return Put(t, k, v)
2126
}
2227

28+
// Size counts nodes in the trie-tree.
2329
func (t *TernaryTrie) Size() int {
2430
count := 0
2531
EachDepth(t, func(Node) bool {
@@ -29,6 +35,7 @@ func (t *TernaryTrie) Size() int {
2935
return count
3036
}
3137

38+
// Balance balances all nodes of trie-tree in each layers.
3239
func (t *TernaryTrie) Balance() {
3340
EachDepth(t, func(n Node) bool {
3441
n.(*TernaryNode).Balance()
@@ -37,17 +44,20 @@ func (t *TernaryTrie) Balance() {
3744
t.root.Balance()
3845
}
3946

47+
// TernaryNode provides node of ternary trie-tree.
4048
type TernaryNode struct {
4149
label rune
4250
firstChild *TernaryNode
4351
low, high *TernaryNode
4452
value interface{}
4553
}
4654

55+
// NewTernaryNode creates a node instance.
4756
func NewTernaryNode(l rune) *TernaryNode {
4857
return &TernaryNode{label: l}
4958
}
5059

60+
// Get returns a child node for k.
5161
func (n *TernaryNode) Get(k rune) Node {
5262
curr := n.firstChild
5363
for curr != nil {
@@ -62,6 +72,7 @@ func (n *TernaryNode) Get(k rune) Node {
6272
return nil
6373
}
6474

75+
// Dig digs a node for k. it returns node and a flag for whether dig or not.
6576
func (n *TernaryNode) Dig(k rune) (node Node, isnew bool) {
6677
curr := n.firstChild
6778
if curr == nil {
@@ -87,14 +98,17 @@ func (n *TernaryNode) Dig(k rune) (node Node, isnew bool) {
8798
}
8899
}
89100

101+
// FirstChild returns first child node.
90102
func (n *TernaryNode) FirstChild() *TernaryNode {
91103
return n.firstChild
92104
}
93105

106+
// HasChildren returns the node hash any children or not.
94107
func (n *TernaryNode) HasChildren() bool {
95108
return n.firstChild != nil
96109
}
97110

111+
// Size counts descended nodes.
98112
func (n *TernaryNode) Size() int {
99113
if n.firstChild == nil {
100114
return 0
@@ -107,6 +121,7 @@ func (n *TernaryNode) Size() int {
107121
return count
108122
}
109123

124+
// Each enumerates descended nodes.
110125
func (n *TernaryNode) Each(proc func(Node) bool) {
111126
var f func(*TernaryNode) bool
112127
f = func(n *TernaryNode) bool {
@@ -120,18 +135,22 @@ func (n *TernaryNode) Each(proc func(Node) bool) {
120135
f(n.firstChild)
121136
}
122137

138+
// RemoveAll removes all descended nodes.
123139
func (n *TernaryNode) RemoveAll() {
124140
n.firstChild = nil
125141
}
126142

143+
// Label returns a label rune.
127144
func (n *TernaryNode) Label() rune {
128145
return n.label
129146
}
130147

148+
// Value returns a value for the node.
131149
func (n *TernaryNode) Value() interface{} {
132150
return n.value
133151
}
134152

153+
// SetValue set a value for the node.
135154
func (n *TernaryNode) SetValue(v interface{}) {
136155
n.value = v
137156
}
@@ -150,6 +169,7 @@ func (n *TernaryNode) children() []*TernaryNode {
150169
return children
151170
}
152171

172+
// Balance balances all descended nodes.
153173
func (n *TernaryNode) Balance() {
154174
if n.firstChild == nil {
155175
return
@@ -171,11 +191,10 @@ func balance(nodes []*TernaryNode, s, e int) *TernaryNode {
171191
} else if count == 2 {
172192
nodes[s].high = nodes[s+1]
173193
return nodes[s]
174-
} else {
175-
mid := (s + e) / 2
176-
n := nodes[mid]
177-
n.low = balance(nodes, s, mid)
178-
n.high = balance(nodes, mid+1, e)
179-
return n
180194
}
195+
mid := (s + e) / 2
196+
n := nodes[mid]
197+
n.low = balance(nodes, s, mid)
198+
n.high = balance(nodes, mid+1, e)
199+
return n
181200
}

ternary_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77

88
func assertNilBoth(t *testing.T, n *TernaryNode) {
99
if n.low != nil {
10-
t.Errorf("low node has value", &n.low)
10+
t.Errorf("low node has value: %+v", &n.low)
1111
}
1212
if n.high != nil {
13-
t.Errorf("high node has value", &n.high)
13+
t.Errorf("high node has value: %+v", &n.high)
1414
}
1515
}
1616

trie.go

+26
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@ import (
44
"container/list"
55
)
66

7+
// Trie provides accessors for trie-tree
78
type Trie interface {
89
Root() Node
910
Get(string) Node
1011
Put(string, interface{}) Node
1112
Size() int
1213
}
1314

15+
// NewTrie creates a new Trie instance.
1416
func NewTrie() Trie {
1517
return NewTernaryTrie()
1618
}
1719

20+
// Get gets a node for k as key.
1821
func Get(t Trie, k string) Node {
1922
if t == nil {
2023
return nil
@@ -29,6 +32,7 @@ func Get(t Trie, k string) Node {
2932
return n
3033
}
3134

35+
// Put puts a pair of key and value then returns the node for it.
3236
func Put(t Trie, k string, v interface{}) Node {
3337
if t == nil {
3438
return nil
@@ -41,6 +45,7 @@ func Put(t Trie, k string, v interface{}) Node {
4145
return n
4246
}
4347

48+
// EachDepth enumerates nodes in trie for depth.
4449
func EachDepth(t Trie, proc func(Node) bool) {
4550
if t == nil {
4651
return
@@ -54,6 +59,7 @@ func EachDepth(t Trie, proc func(Node) bool) {
5459
r.Each(f)
5560
}
5661

62+
// EachWidth enumerates nodes in trie for width.
5763
func EachWidth(t Trie, proc func(Node) bool) {
5864
if t == nil {
5965
return
@@ -74,19 +80,39 @@ func EachWidth(t Trie, proc func(Node) bool) {
7480
}
7581
}
7682

83+
// Node provides accessors for nodes of trie-tree
7784
type Node interface {
85+
86+
// Get returns a child node for k.
7887
Get(k rune) Node
88+
89+
// Dig digs a node for k. it returns node and a flag for whether dig or
90+
// not.
7991
Dig(k rune) (Node, bool)
92+
93+
// HasChildren returns the node hash any children or not.
8094
HasChildren() bool
95+
96+
// Size counts descended nodes.
8197
Size() int
98+
99+
// Each enumerates descended nodes.
82100
Each(func(Node) bool)
101+
102+
// RemoveAll removes all descended nodes.
83103
RemoveAll()
84104

105+
// Label returns a label rune.
85106
Label() rune
107+
108+
// Value returns a value for the node.
86109
Value() interface{}
110+
111+
// SetValue set a value for the node.
87112
SetValue(v interface{})
88113
}
89114

115+
// Children returns all children of the node.
90116
func Children(n Node) []Node {
91117
children := make([]Node, n.Size())
92118
idx := 0

0 commit comments

Comments
 (0)