This repository has been archived by the owner on Nov 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recmgr_test.go
164 lines (159 loc) · 4.13 KB
/
recmgr_test.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
package recmgr_test
import (
"fmt"
"github.com/jung-kurt/recmgr"
"strings"
)
type RecType struct {
name string
num int
}
func (rec RecType) String() string {
numStr := fmt.Sprintf("%d", rec.num)
return fmt.Sprintf("%s%s%s", rec.name, strings.Repeat(".", 18-len(numStr)-len(rec.name)), numStr)
}
func Example_basic() {
var grp recmgr.GrpType
type person struct {
name string
num int
}
personList := []person{
{"Athos", 1},
{"Porthos", 2},
{"Aramis", 3}}
idxName := grp.Index(4, func(a, b interface{}) bool {
return a.(*person).name < b.(*person).name
})
idxNum := grp.Index(4, func(a, b interface{}) bool {
return a.(*person).num < b.(*person).num
})
for j := range personList {
grp.ReplaceOrInsert(&personList[j])
}
print := func(recPtr interface{}) bool {
p := recPtr.(*person)
fmt.Printf(" %-8s %2d\n", p.name, p.num)
return true
}
fmt.Printf("Name order (%d)\n", idxName.Len())
idxName.Ascend(print)
fmt.Printf("Number order (%d)\n", idxNum.Len())
idxNum.Ascend(print)
// Output:
// Name order (3)
// Aramis 3
// Athos 1
// Porthos 2
// Number order (3)
// Athos 1
// Porthos 2
// Aramis 3
}
func Example() {
var rm recmgr.GrpType
recList := []RecType{
{name: "Brahms", num: 1833},
{name: "Bach", num: 1685},
{name: "Palestrina", num: 1525},
{name: "Mozart", num: 1756},
{name: "Schubert", num: 1797},
}
indent := func(fmtStr string, args ...interface{}) {
fmt.Printf(" %s\n", fmt.Sprintf(fmtStr, args...))
}
print := func(recPtr interface{}) bool {
if recPtr != nil {
indent("%s", *recPtr.(*RecType))
}
return true
}
idxName := rm.Index(4, func(a, b interface{}) bool {
return a.(*RecType).name < b.(*RecType).name
})
idxNum := rm.Index(4, func(a, b interface{}) bool {
return a.(*RecType).num < b.(*RecType).num
})
for j := range recList {
rm.ReplaceOrInsert(&recList[j])
}
fmt.Println("Name order")
idxName.Ascend(print)
fmt.Println("Number order")
idxNum.Ascend(print)
fmt.Println("Delete \"Palestrina\"")
indent("keys deleted: %d", rm.Delete(idxName.Get(&RecType{name: "Palestrina"})))
fmt.Println("Name order")
idxName.Ascend(print)
fmt.Println("Number order")
idxNum.Ascend(print)
fmt.Println("Number < 1797")
idxNum.AscendLessThan(&RecType{num: 1797}, print)
fmt.Println("Name >= \"Mozart\"")
idxName.AscendGreaterOrEqual(&RecType{name: "Mozart"}, print)
fmt.Println("1685 <= Number < 1797")
idxNum.AscendRange(&RecType{num: 1685}, &RecType{num: 1797}, print)
fmt.Println("Get 1756")
print(idxNum.Get(&RecType{num: 1756}))
fmt.Println("Get 1800")
print(idxNum.Get(&RecType{num: 1800}))
fmt.Println("Get \"Schubert\"")
print(idxName.Get(&RecType{name: "Schubert"}))
fmt.Println("Get \"Beethoven\"")
print(idxName.Get(&RecType{name: "Beethoven"}))
fmt.Println("Has 1756")
indent("%v", idxNum.Has(&RecType{num: 1756}))
fmt.Println("Has 1770")
indent("%v", idxNum.Has(&RecType{num: 1770}))
fmt.Println("Index count of \"Mozart\"")
indent("%d", rm.Has(idxName.Get(&RecType{name: "Mozart"})))
fmt.Println("Terminated")
idxName.Ascend(func(recPtr interface{}) bool { return false })
// Output:
// Name order
// Bach..........1685
// Brahms........1833
// Mozart........1756
// Palestrina....1525
// Schubert......1797
// Number order
// Palestrina....1525
// Bach..........1685
// Mozart........1756
// Schubert......1797
// Brahms........1833
// Delete "Palestrina"
// keys deleted: 2
// Name order
// Bach..........1685
// Brahms........1833
// Mozart........1756
// Schubert......1797
// Number order
// Bach..........1685
// Mozart........1756
// Schubert......1797
// Brahms........1833
// Number < 1797
// Bach..........1685
// Mozart........1756
// Name >= "Mozart"
// Mozart........1756
// Schubert......1797
// 1685 <= Number < 1797
// Bach..........1685
// Mozart........1756
// Get 1756
// Mozart........1756
// Get 1800
// Get "Schubert"
// Schubert......1797
// Get "Beethoven"
// Has 1756
// true
// Has 1770
// false
// Index count of "Mozart"
// 2
// Terminated
}