Skip to content

Commit c276414

Browse files
author
Tony Lambiris
committed
Apply pull request golang-collections#2 from upstream
1 parent e740680 commit c276414

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

queue/queue.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,25 @@ func (items *items) getUntil(checker func(item interface{}) bool) []interface{}
145145
return returnItems
146146
}
147147

148+
func (items *items) getMatch(checker func(item interface{}) bool) []interface{} {
149+
length := len(*items)
150+
151+
if len(*items) == 0 {
152+
// returning nil here actually wraps that nil in a list
153+
// of interfaces... thanks go
154+
return []interface{}{}
155+
}
156+
157+
returnItems := make([]interface{}, 0, length)
158+
for _, item := range *items {
159+
if !checker(item) {
160+
returnItems = append(returnItems, item)
161+
}
162+
}
163+
164+
return returnItems
165+
}
166+
148167
type sema struct {
149168
ready chan bool
150169
response *sync.WaitGroup
@@ -323,6 +342,51 @@ func (q *Queue) Len() int64 {
323342
return int64(len(q.items))
324343
}
325344

345+
// GetItems returns items in this queue.
346+
func (q *Queue) GetItems() []interface{} {
347+
q.lock.Lock()
348+
defer q.lock.Unlock()
349+
350+
return q.items
351+
}
352+
353+
// Search takes a function and returns a list of items that
354+
// match the checker. This does not wait and remove items.
355+
func (q *Queue) Search(checker func(item interface{}) bool) ([]interface{}) {
356+
if checker == nil {
357+
return nil
358+
}
359+
360+
q.lock.Lock()
361+
362+
if q.disposed {
363+
q.lock.Unlock()
364+
return nil
365+
}
366+
367+
result := q.items.getMatch(checker)
368+
q.lock.Unlock()
369+
return result
370+
}
371+
372+
373+
// GetItem returns one item without deleting in this queue.
374+
func (q *Queue) GetItem(pos int) (interface{}, bool) {
375+
q.lock.Lock()
376+
defer q.lock.Unlock()
377+
if len(q.items) > pos {
378+
return q.items[pos], true
379+
}
380+
return nil, false
381+
}
382+
383+
// GetItems returns items in this queue.
384+
func (q *Queue) Clear(hint int64) {
385+
q.lock.Lock()
386+
defer q.lock.Unlock()
387+
q.items = make([]interface{}, 0, hint)
388+
}
389+
326390
// Disposed returns a bool indicating if this queue
327391
// has had disposed called on it.
328392
func (q *Queue) Disposed() bool {

queue/queue_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,57 @@ func TestGetEmpty(t *testing.T) {
190190
assert.Equal(t, `a`, result[0])
191191
}
192192

193+
func TestGetItems(t *testing.T) {
194+
q := New(10)
195+
196+
q.Put(`a`)
197+
198+
result := q.GetItems()
199+
200+
assert.Len(t, result, 1)
201+
assert.Equal(t, `a`, result[0])
202+
}
203+
204+
func TestSearch(t *testing.T) {
205+
q := New(10)
206+
207+
q.Put(`a`)
208+
q.Put(`b`)
209+
q.Put(`c`)
210+
211+
result := q.Search(func(item interface{}) bool {
212+
return item != `b`
213+
})
214+
215+
assert.Len(t, result, 1)
216+
assert.Equal(t, `b`, result[0])
217+
}
218+
219+
func TestGetItem(t *testing.T) {
220+
q := New(10)
221+
222+
q.Put(`a`)
223+
224+
result, ok := q.GetItem(0)
225+
if !assert.Equal(t, ok, true) {
226+
return
227+
}
228+
229+
assert.Equal(t, `a`, result)
230+
}
231+
232+
func TestClear(t *testing.T) {
233+
q := New(10)
234+
235+
q.Put(`a`)
236+
237+
result := q.GetItems()
238+
assert.Len(t, result, 1)
239+
q.Clear(10)
240+
result = q.GetItems()
241+
assert.Len(t, result, 0)
242+
}
243+
193244
func TestMultipleGetEmpty(t *testing.T) {
194245
q := New(10)
195246
var wg sync.WaitGroup

0 commit comments

Comments
 (0)