aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/internal/vespa/document/queue.go
blob: eed5209ca9e98aea6c46dd1977c31a48e91a1ab0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package document

import (
	"container/list"
)

// Queue is a generic wrapper around a doubly linked list.
type Queue[T any] struct{ items *list.List }

func NewQueue[T any]() *Queue[T] { return &Queue[T]{items: list.New()} }

func (q *Queue[T]) Add(item T, front bool) {
	if front {
		q.items.PushFront(item)
	} else {
		q.items.PushBack(item)
	}
}

func (q *Queue[T]) Poll() (T, bool) {
	front := q.items.Front()
	if front == nil {
		var empty T
		return empty, false
	}
	return q.items.Remove(front).(T), true
}