ds/impl_queue_list.go

48 lines
809 B
Go
Raw Permalink Normal View History

2023-01-20 21:48:46 +01:00
package ds
type ListQueue[T any, L List[T]] struct {
list L
2023-01-20 21:48:46 +01:00
}
var _ Queue[int] = &ListQueue[int, List[int]]{}
2023-01-20 21:48:46 +01:00
func NewListQueue[T any, L List[T]](list L) *ListQueue[T, L] {
return &ListQueue[T, L]{list}
2023-01-20 21:48:46 +01:00
}
func (s *ListQueue[T, L]) Enqueue(value T) {
2023-01-20 21:48:46 +01:00
s.list.Add(value)
}
func (s *ListQueue[T, L]) Add(value T) {
2023-01-20 21:48:46 +01:00
s.Enqueue(value)
}
func (s *ListQueue[T, L]) AddAll(values Iterable[T]) {
2023-01-20 21:48:46 +01:00
values.Each(s.Add)
}
func (s *ListQueue[T, L]) Dequeue() T {
2023-01-20 21:48:46 +01:00
return s.list.RemoveAt(0)
}
func (s *ListQueue[T, L]) Peek() T {
2023-01-20 21:48:46 +01:00
return s.PeekAt(0)
}
func (s *ListQueue[T, L]) PeekAt(index int) T {
2023-01-20 21:48:46 +01:00
return s.list.Get(index)
}
func (s *ListQueue[T, L]) Size() int {
2023-01-20 21:48:46 +01:00
return s.list.Size()
}
func (s *ListQueue[T, L]) Empty() bool {
2023-01-20 21:48:46 +01:00
return s.Size() == 0
}
func (s *ListQueue[T, L]) Clear() {
s.list.Clear()
}