aoc2024/day-20/internal/queue/queue.go

40 lines
642 B
Go
Raw Permalink Normal View History

2024-12-20 20:48:13 +00:00
package queue
import "sync"
type ConcurrentQueue[T comparable] struct {
items []T
lock sync.Mutex
cond *sync.Cond
}
func New[T comparable]() *ConcurrentQueue[T] {
q := &ConcurrentQueue[T]{}
q.cond = sync.NewCond(&q.lock)
return q
}
func (q *ConcurrentQueue[T]) Enqueue(item T) {
q.lock.Lock()
defer q.lock.Unlock()
q.items = append(q.items, item)
q.cond.Signal()
}
func (q *ConcurrentQueue[T]) Dequeue() T {
q.lock.Lock()
defer q.lock.Unlock()
for len(q.items) == 0 {
q.cond.Wait()
}
item := q.items[0]
q.items = q.items[1:]
return item
}
func (q *ConcurrentQueue[T]) IsEmpty() bool {
return len(q.items) == 0
}