-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqueue.go
55 lines (48 loc) · 1.29 KB
/
queue.go
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package immutable
func queueRotate[T any](f *lazyList[T], r *Stack[T], s *lazyList[T]) *lazyList[T] {
if f == nil {
return s.PushFront(r.Peek())
}
return newLazyList(f.Front(), func() *lazyList[T] {
return queueRotate(f.PopFront(), r.Pop(), s.PushFront(r.Peek()))
})
}
func queueExec[T any](f *lazyList[T], r *Stack[T], s *lazyList[T]) *Queue[T] {
if s == nil {
f2 := queueRotate(f, r, nil)
return &Queue[T]{f2, nil, f2}
}
return &Queue[T]{f, r, s.PopFront()}
}
// Queue implements a first in, first out container.
//
// Nil and the zero value for Queue are both empty queues.
type Queue[T any] struct {
f *lazyList[T]
r *Stack[T]
s *lazyList[T]
}
// Empty returns true if the queue is empty.
//
// Complexity: O(1) worst-case
func (q *Queue[T]) Empty() bool {
return q == nil || q.f == nil
}
// Front returns the item at the front of the queue.
//
// Complexity: O(1) worst-case
func (q *Queue[T]) Front() T {
return q.f.Front()
}
// PopFront removes the item at the front of the queue.
//
// Complexity: O(1) worst-case
func (q *Queue[T]) PopFront() *Queue[T] {
return queueExec(q.f.PopFront(), q.r, q.s)
}
// PushBack pushes an item onto the back of the queue.
//
// Complexity: O(1) worst-case
func (q *Queue[T]) PushBack(value T) *Queue[T] {
return queueExec(q.f, q.r.Push(value), q.s)
}