sized_queue.go 527 B

123456789101112131415161718192021222324
  1. package collect
  2. type SizedQueue struct {
  3. elements []interface{}
  4. nextPos int
  5. }
  6. func NewSizedQueue(size int) *SizedQueue {
  7. return &SizedQueue{
  8. elements: make([]interface{}, size),
  9. nextPos: 0,
  10. }
  11. }
  12. // Put puts a new element into the queue and pop out the first element if queue is full.
  13. func (this *SizedQueue) Put(element interface{}) interface{} {
  14. res := this.elements[this.nextPos]
  15. this.elements[this.nextPos] = element
  16. this.nextPos++
  17. if this.nextPos == len(this.elements) {
  18. this.nextPos = 0
  19. }
  20. return res
  21. }