/
list.go
94 lines (84 loc) · 1.4 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
package utils
import (
"container/list"
"sync"
)
type List struct {
l *list.List
len int
mux sync.Mutex
ch chan byte
}
func NewList() *List {
return &List{
l: list.New(),
ch: make(chan byte, 1),
}
}
func (this *List) Push(v interface{}) {
if v == nil {
panic(nil)
}
this.mux.Lock()
this.len++
this.l.PushBack(v)
this.mux.Unlock()
select {
case this.ch <- 0:
default:
}
}
func (this *List) Pop() interface{} {
this.mux.Lock()
defer this.mux.Unlock()
if this.l.Len() > 0 {
v := this.l.Remove(this.l.Front())
if this.l.Len() > 0 {
select {
case this.ch <- 0:
default:
}
}
return v
}
return nil
}
func (this *List) Len() int {
this.mux.Lock()
defer this.mux.Unlock()
return this.l.Len()
}
//遍历列表
//cb返回true时停止遍历,false继续遍历
func (this *List) Each(cb func(v interface{}) (stop bool)) {
this.mux.Lock()
defer this.mux.Unlock()
for f := this.l.Front(); f != nil; f = f.Next() {
stop := cb(f.Value)
if stop {
break
}
}
}
//删除元素
//cb返回true时删除当前元素
func (this *List) Remove(cb func(v interface{}) (del, c bool)) {
this.mux.Lock()
defer this.mux.Unlock()
for f := this.l.Front(); f != nil; {
del, _continue := cb(f.Value)
if del {
n := f.Next()
this.l.Remove(f)
f = n
} else {
f = f.Next()
}
if !_continue {
break
}
}
}
func (this *List) Wait() <-chan byte {
return this.ch
}