Skip to content

Commit e7c5d1f

Browse files
author
Tony Lambiris
committed
Apply pull request golang-collections#2 from upstream
1 parent 59788d5 commit e7c5d1f

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

queue/queue.go

+64
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,25 @@ func (items *items) getUntil(checker func(item interface{}) bool) []interface{}
118118
return returnItems
119119
}
120120

121+
func (items *items) getMatch(checker func(item interface{}) bool) []interface{} {
122+
length := len(*items)
123+
124+
if len(*items) == 0 {
125+
// returning nil here actually wraps that nil in a list
126+
// of interfaces... thanks go
127+
return []interface{}{}
128+
}
129+
130+
returnItems := make([]interface{}, 0, length)
131+
for _, item := range *items {
132+
if !checker(item) {
133+
returnItems = append(returnItems, item)
134+
}
135+
}
136+
137+
return returnItems
138+
}
139+
121140
type sema struct {
122141
wg *sync.WaitGroup
123142
response *sync.WaitGroup
@@ -246,6 +265,51 @@ func (q *Queue) Len() int64 {
246265
return int64(len(q.items))
247266
}
248267

268+
// GetItems returns items in this queue.
269+
func (q *Queue) GetItems() []interface{} {
270+
q.lock.Lock()
271+
defer q.lock.Unlock()
272+
273+
return q.items
274+
}
275+
276+
// Search takes a function and returns a list of items that
277+
// match the checker. This does not wait and remove items.
278+
func (q *Queue) Search(checker func(item interface{}) bool) ([]interface{}) {
279+
if checker == nil {
280+
return nil
281+
}
282+
283+
q.lock.Lock()
284+
285+
if q.disposed {
286+
q.lock.Unlock()
287+
return nil
288+
}
289+
290+
result := q.items.getMatch(checker)
291+
q.lock.Unlock()
292+
return result
293+
}
294+
295+
296+
// GetItem returns one item without deleting in this queue.
297+
func (q *Queue) GetItem(pos int) (interface{}, bool) {
298+
q.lock.Lock()
299+
defer q.lock.Unlock()
300+
if len(q.items) > pos {
301+
return q.items[pos], true
302+
}
303+
return nil, false
304+
}
305+
306+
// GetItems returns items in this queue.
307+
func (q *Queue) Clear(hint int64) {
308+
q.lock.Lock()
309+
defer q.lock.Unlock()
310+
q.items = make([]interface{}, 0, hint)
311+
}
312+
249313
// Disposed returns a bool indicating if this queue
250314
// has had disposed called on it.
251315
func (q *Queue) Disposed() bool {

queue/queue_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,57 @@ func TestGetEmpty(t *testing.T) {
134134
assert.Equal(t, `a`, result[0])
135135
}
136136

137+
func TestGetItems(t *testing.T) {
138+
q := New(10)
139+
140+
q.Put(`a`)
141+
142+
result := q.GetItems()
143+
144+
assert.Len(t, result, 1)
145+
assert.Equal(t, `a`, result[0])
146+
}
147+
148+
func TestSearch(t *testing.T) {
149+
q := New(10)
150+
151+
q.Put(`a`)
152+
q.Put(`b`)
153+
q.Put(`c`)
154+
155+
result := q.Search(func(item interface{}) bool {
156+
return item != `b`
157+
})
158+
159+
assert.Len(t, result, 1)
160+
assert.Equal(t, `b`, result[0])
161+
}
162+
163+
func TestGetItem(t *testing.T) {
164+
q := New(10)
165+
166+
q.Put(`a`)
167+
168+
result, ok := q.GetItem(0)
169+
if !assert.Equal(t, ok, true) {
170+
return
171+
}
172+
173+
assert.Equal(t, `a`, result)
174+
}
175+
176+
func TestClear(t *testing.T) {
177+
q := New(10)
178+
179+
q.Put(`a`)
180+
181+
result := q.GetItems()
182+
assert.Len(t, result, 1)
183+
q.Clear(10)
184+
result = q.GetItems()
185+
assert.Len(t, result, 0)
186+
}
187+
137188
func TestMultipleGetEmpty(t *testing.T) {
138189
q := New(10)
139190
var wg sync.WaitGroup

0 commit comments

Comments
 (0)