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

41 lines
670 B
Go

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()
}
// Gets the item from queue
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
}