-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharray.go
153 lines (122 loc) · 2.45 KB
/
array.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
package utils
import "sync"
type Array interface {
//Push 向链表尾部添加一个或者多个元素
Push(values ...interface{})
// Pop 从列表尾部移除并返回一个元素
Pop() interface{}
//PushFront 向链表头部添加一个元素
PushFront(values ...interface{})
// PopFront从链表头部移除并返回一个元素
PopFront() interface{}
// Len 返回集链表元素的长度
Len() int
// Values 返回链表所有元素组成的 Slice
Values() []interface{}
// Iter 遍历链表的所有元素
Iter() <-chan interface{}
// Clear 清空链表
Clear()
}
type array struct {
m []interface{}
rw sync.RWMutex
block bool
}
// NewArray 创建一个链表,block == true :线程安全,block == false:非线程安全
func NewArray(block bool, values ...interface{}) Array {
var s = &array{}
s.block = block
if len(values) > 0 {
s.Push(values...)
}
return s
}
func (this *array) lock() {
if this.block {
this.rw.Lock()
}
}
func (this *array) unlock() {
if this.block {
this.rw.Unlock()
}
}
func (this *array) rLock() {
if this.block {
this.rw.RLock()
}
}
func (this *array) rUnlock() {
if this.block {
this.rw.RUnlock()
}
}
func (this *array) Push(values ...interface{}) {
this.lock()
defer this.unlock()
this.m = append(this.m, values...)
}
func (this *array) PushFront(values ...interface{}) {
this.lock()
defer this.unlock()
var tmp []interface{}
tmp = append(tmp, values...)
this.m = append(tmp, this.m...)
}
func (this *array) PopFront() interface{} {
this.lock()
defer this.unlock()
if this.len() > 0 {
elem := this.m[0]
this.m = this.m[1:]
return elem
}
return nil
}
func (this *array) Pop() interface{} {
this.lock()
defer this.unlock()
if this.len() > 0 {
elem := this.m[this.len()-1]
this.m = this.m[:this.len()-1]
return elem
}
return nil
}
func (this *array) Len() int {
this.rLock()
defer this.rUnlock()
return this.len()
}
func (this *array) len() int {
return len(this.m)
}
func (this *array) Values() []interface{} {
this.rLock()
defer this.rUnlock()
tmp := make([]interface{}, this.len())
if this.len() > 0 {
copy(tmp, this.m)
}
return tmp
}
func (this *array) Iter() <-chan interface{} {
var ch = make(chan interface{})
go func(s *array) {
if s.block {
s.rLock()
}
for i := 0; i < s.len(); i++ {
ch <- this.m[i]
}
close(ch)
if s.block {
s.rUnlock()
}
}(this)
return ch
}
func (this *array) Clear() {
this.m = []interface{}{}
}