@@ -164,6 +164,25 @@ func (items *items) getMatch(checker func(item interface{}) bool) []interface{}
164164 return returnItems
165165}
166166
167+ func (items * items ) getMatch (checker func (item interface {}) bool ) []interface {} {
168+ length := len (* items )
169+
170+ if len (* items ) == 0 {
171+ // returning nil here actually wraps that nil in a list
172+ // of interfaces... thanks go
173+ return []interface {}{}
174+ }
175+
176+ returnItems := make ([]interface {}, 0 , length )
177+ for _ , item := range * items {
178+ if ! checker (item ) {
179+ returnItems = append (returnItems , item )
180+ }
181+ }
182+
183+ return returnItems
184+ }
185+
167186type sema struct {
168187 ready chan bool
169188 response * sync.WaitGroup
@@ -369,6 +388,51 @@ func (q *Queue) Search(checker func(item interface{}) bool) []interface{} {
369388 return result
370389}
371390
391+ // GetItem returns one item without deleting in this queue.
392+ func (q * Queue ) GetItem (pos int ) (interface {}, bool ) {
393+ q .lock .Lock ()
394+ defer q .lock .Unlock ()
395+ if len (q .items ) > pos {
396+ return q .items [pos ], true
397+ }
398+ return nil , false
399+ }
400+
401+ // GetItems returns items in this queue.
402+ func (q * Queue ) Clear (hint int64 ) {
403+ q .lock .Lock ()
404+ defer q .lock .Unlock ()
405+ q .items = make ([]interface {}, 0 , hint )
406+ }
407+
408+ // GetItems returns items in this queue.
409+ func (q * Queue ) GetItems () []interface {} {
410+ q .lock .Lock ()
411+ defer q .lock .Unlock ()
412+
413+ return q .items
414+ }
415+
416+ // Search takes a function and returns a list of items that
417+ // match the checker. This does not wait and remove items.
418+ func (q * Queue ) Search (checker func (item interface {}) bool ) ([]interface {}) {
419+ if checker == nil {
420+ return nil
421+ }
422+
423+ q .lock .Lock ()
424+
425+ if q .disposed {
426+ q .lock .Unlock ()
427+ return nil
428+ }
429+
430+ result := q .items .getMatch (checker )
431+ q .lock .Unlock ()
432+ return result
433+ }
434+
435+
372436// GetItem returns one item without deleting in this queue.
373437func (q * Queue ) GetItem (pos int ) (interface {}, bool ) {
374438 q .lock .Lock ()
0 commit comments