/
queue.go
189 lines (152 loc) · 2.59 KB
/
queue.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
package goutil
import (
"sort"
"sync"
)
/// List
type fnListLess func(a, b interface{}) bool
type List struct {
Queue
less fnListLess
}
func NewList(less fnListLess) *List {
return &List{
less: less,
}
}
func (t *List) Add(e interface{}) {
t.Lock()
defer t.Unlock()
t.l = append(t.l, e)
if t.less != nil {
max_idx := len(t.l) - 1
for idx := max_idx - 1; idx >= 0; idx -= 1 {
item := t.l[idx]
if t.less(item, e) { // stable sort
break
} else {
t.l[idx+1] = item
t.l[idx] = e
}
}
}
}
func (t *List) Sort(less fnListLess) {
t.Lock()
defer t.Unlock()
if less != nil {
old := t.less
t.less = less
sort.Sort(t)
t.less = old
}
}
func (t *List) SortStable(less fnListLess) {
t.Lock()
defer t.Unlock()
if less != nil {
old := t.less
t.less = less
sort.Stable(t)
t.less = old
}
}
// @sort only and no mutex here
func (t *List) Less(i, j int) bool {
if t.less != nil {
return t.less(t.l[i], t.l[j])
} else {
return true
}
}
// @sort only and no mutex here
func (t *List) Swap(i, j int) {
tmp := t.l[i]
t.l[i] = t.l[j]
t.l[j] = tmp
}
/// Queue
type Queue struct {
sync.Mutex
l []interface{}
}
func NewQueue() *Queue {
return &Queue{}
}
func (q *Queue) Len() int {
q.Lock()
defer q.Unlock()
return len(q.l)
}
func (q *Queue) Get(idx int) interface{} {
q.Lock()
defer q.Unlock()
if idx >= 0 && idx < len(q.l) {
return q.l[idx]
}
return nil
}
func (q *Queue) Empty() bool {
q.Lock()
defer q.Unlock()
return len(q.l) == 0
}
func (q *Queue) Clear() {
q.Lock()
defer q.Unlock()
q.l = q.l[0:0]
}
// Add a new element to the end, after its current last element.
func (q *Queue) PushBack(e interface{}) {
q.Lock()
defer q.Unlock()
q.l = append(q.l, e)
}
// Pop the last element
func (q *Queue) PopBack() {
q.Lock()
defer q.Unlock()
if len(q.l) != 0 {
size := len(q.l) - 1
q.l = q.l[:size]
}
}
// Access the last element
func (q *Queue) Back() interface{} {
q.Lock()
defer q.Unlock()
if len(q.l) != 0 {
idx := len(q.l) - 1
return q.l[idx]
}
return nil
}
// Insert a new element to the front, before its current first element.
func (q *Queue) PushFront(e interface{}) {
q.Lock()
defer q.Unlock()
var tmpl []interface{}
tmpl = append(tmpl, e)
q.l = append(tmpl, q.l...)
}
// Pop the first element
func (q *Queue) PopFront() {
q.Lock()
defer q.Unlock()
if len(q.l) != 0 {
q.l = q.l[1:]
}
}
// Access the first element
func (q *Queue) Front() interface{} {
q.Lock()
defer q.Unlock()
if len(q.l) != 0 {
return q.l[0]
}
return nil
}
// Access values @unsafe
func (q *Queue) Values() []interface{} {
return q.l
}