-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
175 lines (131 loc) · 2.52 KB
/
example_test.go
File metadata and controls
175 lines (131 loc) · 2.52 KB
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
package ordered_test
import (
"fmt"
"go.treyburn.dev/ordered"
)
func ExampleNewMap() {
m := ordered.NewMap[string, int]()
m.Set("a", 1)
m.Set("b", 2)
m.Set("c", 3)
fmt.Println(m.Len())
fmt.Println(m.Get("a"))
fmt.Println(m.Get("missing"))
// Output:
// 3
// 1 true
// 0 false
}
func ExampleMap_Set() {
m := ordered.NewMap[string, int]()
m.Set("a", 1)
m.Set("b", 2)
m.Set("c", 3)
// Set on an existing key updates the value but preserves order
m.Set("a", 10)
for k, v := range m.Entries() {
fmt.Printf("%s:%d ", k, v)
}
fmt.Println()
// Output:
// a:10 b:2 c:3
}
func ExampleMap_Push() {
m := ordered.NewMap[string, int]()
m.Set("a", 1)
m.Set("b", 2)
m.Set("c", 3)
// Push on an existing key updates the value and moves it to the back
m.Push("a", 10)
for k, v := range m.Entries() {
fmt.Printf("%s:%d ", k, v)
}
fmt.Println()
// Output:
// b:2 c:3 a:10
}
func ExampleMap_Pop() {
m := ordered.NewMap[string, int]()
m.Set("a", 1)
m.Set("b", 2)
m.Set("c", 3)
v, ok := m.Pop("b")
fmt.Println(v, ok)
v, ok = m.Pop("missing")
fmt.Println(v, ok)
for k, v := range m.Entries() {
fmt.Printf("%s:%d ", k, v)
}
fmt.Println()
// Output:
// 2 true
// 0 false
// a:1 c:3
}
func ExampleMap_Insert() {
m := ordered.NewMap[string, int]()
m.Set("a", 1)
m.Set("b", 2)
m.Set("c", 3)
// Insert on an existing key updates the value and moves it to the front
m.Insert("c", 30)
for k, v := range m.Entries() {
fmt.Printf("%s:%d ", k, v)
}
fmt.Println()
// Output:
// c:30 a:1 b:2
}
func ExampleMap_Insert_newKey() {
m := ordered.NewMap[string, int]()
m.Set("a", 1)
m.Set("b", 2)
// Insert a new key: added to the front
m.Insert("c", 3)
for k, v := range m.Entries() {
fmt.Printf("%s:%d ", k, v)
}
fmt.Println()
// Output:
// c:3 a:1 b:2
}
func ExampleNewMap_withCapacity() {
m := ordered.NewMap[string, int](ordered.WithCapacity(10))
m.Set("a", 1)
m.Set("b", 2)
m.Set("c", 3)
fmt.Println(m.Len())
for k, v := range m.Entries() {
fmt.Printf("%s:%d ", k, v)
}
fmt.Println()
// Output:
// 3
// a:1 b:2 c:3
}
func ExampleMap_Entries() {
m := ordered.NewMap[string, int]()
m.Set("a", 1)
m.Set("b", 2)
m.Set("c", 3)
for k, v := range m.Entries() {
fmt.Printf("%s => %d\n", k, v)
}
// Output:
// a => 1
// b => 2
// c => 3
}
func ExampleMap_Reverse() {
m := ordered.NewMap[string, int]()
m.Set("a", 1)
m.Set("b", 2)
m.Set("c", 3)
for k, v := range m.Reverse().Entries() {
fmt.Printf("%s => %d\n", k, v)
}
// Output:
// c => 3
// b => 2
// a => 1
}