-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
202 lines (180 loc) · 3.7 KB
/
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
package ds
import (
"fmt"
"github.com/qizong007/ware-kv/warekv/util"
"reflect"
"sync"
"unsafe"
)
type List struct {
Base
list *[]interface{}
rw sync.RWMutex
}
var listStructMemUsage int
func init() {
listStructMemUsage = int(unsafe.Sizeof(List{}))
}
func (l *List) GetValue() interface{} {
val := l.listView()
return val
}
// deep copy
func (l *List) listView() []interface{} {
l.rw.RLock()
defer l.rw.RUnlock()
list := make([]interface{}, len(*l.list))
for i, e := range *l.list {
list[i] = e
}
return list
}
func (l *List) Size() int {
size := listStructMemUsage
if l.ExpireTime != nil {
size += 8
}
l.rw.RLock()
defer l.rw.RUnlock()
if l.list == nil || len(*l.list) == 0 {
return size
}
if rSize := util.GetRealSizeOf(*l.list); rSize > 0 {
size += rSize
}
return size
}
func MakeList(list []interface{}) *List {
return &List{
Base: *NewBase(util.ListDS),
list: &list,
}
}
func (l *List) GetListBetween(left int, right int) ([]interface{}, error) {
l.rw.RLock()
defer l.rw.RUnlock()
lLen := l.GetLen()
if left < 0 || left >= lLen || right < 0 {
return nil, fmt.Errorf("array out of bounds")
}
if left >= right {
return nil, fmt.Errorf("left should not be larger than(or equal to) right")
}
if right >= lLen {
right = lLen
}
list := l.listView()
return list[left:right], nil
}
func (l *List) GetListStartWith(left int) ([]interface{}, error) {
l.rw.RLock()
defer l.rw.RUnlock()
lLen := l.GetLen()
if left < 0 || left >= lLen {
return nil, fmt.Errorf("array out of bounds")
}
list := l.listView()
return list[left:], nil
}
func (l *List) GetListEndAt(right int) ([]interface{}, error) {
l.rw.RLock()
defer l.rw.RUnlock()
lLen := l.GetLen()
if right < 0 || right >= lLen {
return nil, fmt.Errorf("array out of bounds")
}
list := l.listView()
return list[:right+1], nil
}
func (l *List) GetElementAt(pos int) (interface{}, error) {
l.rw.RLock()
defer l.rw.RUnlock()
if pos < 0 || pos >= l.GetLen() {
return nil, fmt.Errorf("pos out of bounds")
}
list := l.listView()
return list[pos], nil
}
func (l *List) GetListEqualToVal(val interface{}) []interface{} {
list := make([]interface{}, 0)
l.rw.RLock()
defer l.rw.RUnlock()
for _, v := range *l.list {
if v == val {
list = append(list, v)
}
}
return list
}
func (l *List) Append(list []interface{}) {
l.rw.Lock()
defer l.rw.Unlock()
l.Update()
*l.list = append(*l.list, list...)
}
func (l *List) SetAt(idx int, val interface{}) error {
if idx < 0 || idx >= l.GetLen() {
return fmt.Errorf("idx out of bounds")
}
l.rw.Lock()
defer l.rw.Unlock()
l.Update()
(*l.list)[idx] = val
return nil
}
func (l *List) RemoveAt(idx int) error {
if idx < 0 || idx >= l.GetLen() {
return fmt.Errorf("idx out of bounds")
}
l.rw.Lock()
defer l.rw.Unlock()
l.Update()
for i := range *l.list {
if i == idx {
*l.list = append((*l.list)[:i], (*l.list)[i+1:]...)
break
}
}
return nil
}
func (l *List) RemoveVal(val interface{}) {
l.rw.Lock()
defer l.rw.Unlock()
l.Update()
for i, v := range *l.list {
if reflect.DeepEqual(v, val) {
*l.list = append((*l.list)[:i], (*l.list)[i+1:]...)
}
}
}
func (l *List) GetLen() int {
l.rw.RLock()
defer l.rw.RUnlock()
return len(*l.list)
}
func (l *List) RPush(ele interface{}) {
l.Append([]interface{}{ele})
}
func (l *List) RPop() interface{} {
l.rw.Lock()
defer l.rw.Unlock()
l.Update()
last := len(*l.list) - 1
res := (*l.list)[last]
*l.list = (*l.list)[:last]
return res
}
func (l *List) LPush(ele interface{}) {
l.rw.Lock()
defer l.rw.Unlock()
l.Update()
*l.list = append([]interface{}{ele}, *l.list...)
}
func (l *List) LPop() interface{} {
l.rw.Lock()
defer l.rw.Unlock()
l.Update()
res := (*l.list)[0]
*l.list = (*l.list)[1:]
return res
}