-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy patht_list.go
260 lines (235 loc) · 4.93 KB
/
t_list.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package redis
import (
"container/list"
"github.com/pkg/errors"
)
const ListType = uint64(1)
const ListTypeFancy = "list"
var _ Item = (*List)(nil)
type List struct {
goList *list.List
}
func NewList() *List {
return &List{goList: list.New()}
}
func (l *List) Value() interface{} {
return l.goList
}
func (l *List) Type() uint64 {
return ListType
}
func (l *List) TypeFancy() string {
return ListTypeFancy
}
func (l *List) OnDelete(key *string, db *RedisDb) {
panic("implement me")
}
// LLen returns number of elements.
func (l *List) LLen() int {
return l.goList.Len()
}
// LPush returns the length of the list after the push operation.
func (l *List) LPush(values ...*string) int {
for _, v := range values {
l.goList.PushFront(*v)
}
return l.LLen()
}
// RPush returns the length of the list after the push operation.
func (l *List) RPush(values ...*string) int {
for _, v := range values {
l.goList.PushBack(*v)
}
return l.LLen()
}
// LInsert see redis doc
func (l *List) LInsert(isBefore bool, pivot, value *string) int {
for e := l.goList.Front(); e.Next() != nil; e = e.Next() {
if *vts(e) == *pivot {
if isBefore {
l.goList.InsertBefore(*value, e)
} else {
l.goList.InsertAfter(*value, e)
}
return l.LLen()
}
}
return -1
}
// LPop returns popped value and false -
// returns true if list is now emptied so the key can be deleted.
func (l *List) LPop() (*string, bool) {
if e := l.goList.Front(); e == nil {
return nil, true
} else {
l.goList.Remove(e)
return vts(e), false
}
}
// RPop returns popped value and false -
// returns true if list is now emptied so the key can be deleted.
func (l *List) RPop() (*string, bool) {
if e := l.goList.Back(); e == nil {
return nil, true
} else {
l.goList.Remove(e)
return vts(e), false
}
}
// LRem see redis doc
func (l *List) LRem(count int, value *string) int {
// count > 0: Remove elements equal to value moving from head to tail.
// count < 0: Remove elements equal to value moving from tail to head.
// count = 0: Remove all elements equal to value.
var rem int
if count >= 0 {
for e := l.goList.Front(); e.Next() != nil; {
if *vts(e) == *value {
r := e
e = e.Next()
l.goList.Remove(r)
rem++
if count != 0 && rem == count {
break
}
} else {
e = e.Next()
}
}
} else if count < 0 {
count = abs(count)
for e := l.goList.Back(); e.Prev() != nil; {
if *vts(e) == *value {
r := e
e = e.Prev()
l.goList.Remove(r)
rem++
if count != 0 && rem == count {
break
}
} else {
e = e.Prev()
}
}
}
return rem
}
// LSet see redis doc
func (l *List) LSet(index int, value *string) error {
e := atIndex(index, l.goList)
if e == nil {
return errors.New("index out of range")
}
e.Value = *value
return nil
}
// LIndex see redis doc
func (l *List) LIndex(index int) (*string, error) {
e := atIndex(index, l.goList)
if e == nil {
return nil, errors.New("index out of range")
}
return vts(e), nil
}
// LRange see redis doc
func (l *List) LRange(start int, end int) []string {
values := make([]string, 0)
// from index to index
from, to := startEndIndexes(start, end, l.LLen())
if from > to {
return values
}
// get start element
e := atIndex(from, l.goList)
if e == nil { // shouldn't happen
return values
}
// fill with values
values = append(values, *vts(e))
for i := 0; i < to; i++ {
e = e.Next()
values = append(values, *vts(e))
}
return values
}
// LTrim see redis docs - returns true if list is now emptied so the key can be deleted.
func (l *List) LTrim(start int, end int) bool {
// from index to index
from, to := startEndIndexes(start, end, l.LLen())
if from > to {
l.goList.Init()
return true
}
// trim before
if from > 0 {
i := 0
e := l.goList.Front()
for e != nil && i < from {
del := e
e = e.Next()
l.goList.Remove(del)
i++
}
}
// trim after
if to < l.LLen() {
i := l.LLen()
e := l.goList.Back()
for e != nil && i > to {
del := e
e = e.Prev()
l.goList.Remove(del)
i--
}
}
return false
}
func startEndIndexes(start, end int, listLen int) (int, int) {
if end > listLen-1 {
end = listLen - 1
}
return toIndex(start, listLen), toIndex(end, listLen)
}
// atIndex finds element at given index or nil.
func atIndex(index int, list *list.List) *list.Element {
index = toIndex(index, list.Len())
e, i := list.Front(), 0
for ; e.Next() != nil && i < index; i++ {
if e.Next() == nil {
return nil
}
e = e.Next()
}
return e
}
// Converts to real index.
//
// E.g. i=5, len=10 -> returns 5
//
// E.g. i=-1, len=10 -> returns 10
//
// E.g. i=-10, len=10 -> returns 0
//
// E.g. i=-3, len=10 -> returns 7
func toIndex(i int, len int) int {
if i < 0 {
if len+i > 0 {
return len + i
} else {
return 0
}
}
return i
}
// Value of a list element to string.
func vts(e *list.Element) *string {
v := e.Value.(string)
return &v
}
// Return positive x
func abs(x int) int {
if x < 0 {
return -x
}
return x
}