gitlab.com/beacon-software/gadget@v0.0.0-20181217202115-54565ea1ed5e/collection/specialized/requeingqueue.go (about) 1 package specialized 2 3 import "gitlab.com/beacon-software/gadget/collection" 4 5 // NewRequeueingQueue for a queue that never loses elements, they are just 6 // added back onto the end of the queue on pop. Useful for when you don't want 7 // to keep track of an array and an index on an object. 8 func NewRequeueingQueue() collection.Stack { 9 return &requeueingQueue{list: collection.NewDList()} 10 } 11 12 type requeueingQueue struct { 13 list collection.DList 14 } 15 16 // Size of the queue represented as a count of the elements in the queue. 17 func (q *requeueingQueue) Size() int { return q.list.Size() } 18 19 // Push a new data element onto the queue. 20 func (q *requeueingQueue) Push(data interface{}) { 21 q.list.InsertPrevious(q.list.Head(), data) 22 } 23 24 // Pop the most recently pushed data element off the queue and put it at the end of the queue. 25 func (q *requeueingQueue) Pop() (data interface{}, err error) { 26 head := q.list.Head() 27 if nil == head { 28 return nil, collection.NewEmptyListError() 29 } 30 q.list.InsertNext(q.list.Tail(), head.Data()) 31 return q.list.Remove(head) 32 } 33 34 // Peek returns the most recently pushed element without modifying the queue 35 func (q requeueingQueue) Peek() (interface{}, error) { 36 if q.list.Size() == 0 { 37 return nil, collection.NewEmptyListError() 38 } 39 return q.list.Head().Data(), nil 40 }