2023-01-20 21:48:46 +01:00
|
|
|
package ds
|
|
|
|
|
2023-07-10 17:16:41 +02:00
|
|
|
type ListQueue[T any, L List[T]] struct {
|
|
|
|
list L
|
2023-01-20 21:48:46 +01:00
|
|
|
}
|
|
|
|
|
2023-07-10 17:16:41 +02:00
|
|
|
var _ Queue[int] = &ListQueue[int, List[int]]{}
|
2023-01-20 21:48:46 +01:00
|
|
|
|
2023-07-10 17:16:41 +02: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
|
|
|
}
|
|
|
|
|
2023-07-10 17:16:41 +02:00
|
|
|
func (s *ListQueue[T, L]) Enqueue(value T) {
|
2023-01-20 21:48:46 +01:00
|
|
|
s.list.Add(value)
|
|
|
|
}
|
|
|
|
|
2023-07-10 17:16:41 +02:00
|
|
|
func (s *ListQueue[T, L]) Add(value T) {
|
2023-01-20 21:48:46 +01:00
|
|
|
s.Enqueue(value)
|
|
|
|
}
|
|
|
|
|
2023-07-10 17:16:41 +02:00
|
|
|
func (s *ListQueue[T, L]) AddAll(values Iterable[T]) {
|
2023-01-20 21:48:46 +01:00
|
|
|
values.Each(s.Add)
|
|
|
|
}
|
|
|
|
|
2023-07-10 17:16:41 +02:00
|
|
|
func (s *ListQueue[T, L]) Dequeue() T {
|
2023-01-20 21:48:46 +01:00
|
|
|
return s.list.RemoveAt(0)
|
|
|
|
}
|
|
|
|
|
2023-07-10 17:16:41 +02:00
|
|
|
func (s *ListQueue[T, L]) Peek() T {
|
2023-01-20 21:48:46 +01:00
|
|
|
return s.PeekAt(0)
|
|
|
|
}
|
|
|
|
|
2023-07-10 17:16:41 +02:00
|
|
|
func (s *ListQueue[T, L]) PeekAt(index int) T {
|
2023-01-20 21:48:46 +01:00
|
|
|
return s.list.Get(index)
|
|
|
|
}
|
|
|
|
|
2023-07-10 17:16:41 +02:00
|
|
|
func (s *ListQueue[T, L]) Size() int {
|
2023-01-20 21:48:46 +01:00
|
|
|
return s.list.Size()
|
|
|
|
}
|
|
|
|
|
2023-07-10 17:16:41 +02:00
|
|
|
func (s *ListQueue[T, L]) Empty() bool {
|
2023-01-20 21:48:46 +01:00
|
|
|
return s.Size() == 0
|
|
|
|
}
|
2023-01-20 22:12:17 +01:00
|
|
|
|
2023-07-10 17:16:41 +02:00
|
|
|
func (s *ListQueue[T, L]) Clear() {
|
2023-01-20 22:12:17 +01:00
|
|
|
s.list.Clear()
|
|
|
|
}
|