diff --git a/internal/queue/queue.go b/internal/queue/queue.go new file mode 100644 index 0000000..1396e66 --- /dev/null +++ b/internal/queue/queue.go @@ -0,0 +1,55 @@ +// Package queue provides a thread-safe FIFO queue implementation. +package queue + +import "sync" + +type Queue[T any] interface { + Enqueue(item T) + Dequeue() (T, bool) + IsEmpty() bool + Size() int +} + +type queue[T any] struct { + items []T + lock sync.Mutex +} + +func New[T any]() Queue[T] { + return &queue[T]{items: []T{}} +} + +func (q *queue[T]) Enqueue(item T) { + q.lock.Lock() + defer q.lock.Unlock() + + q.items = append(q.items, item) +} + +func (q *queue[T]) Dequeue() (T, bool) { + q.lock.Lock() + defer q.lock.Unlock() + + if len(q.items) == 0 { + var zero T + return zero, false + } + + item := q.items[0] + q.items = q.items[1:] + return item, true +} + +func (q *queue[T]) IsEmpty() bool { + q.lock.Lock() + defer q.lock.Unlock() + + return len(q.items) == 0 +} + +func (q *queue[T]) Size() int { + q.lock.Lock() + defer q.lock.Unlock() + + return len(q.items) +} diff --git a/internal/queue/queue_test.go b/internal/queue/queue_test.go new file mode 100644 index 0000000..b5079ed --- /dev/null +++ b/internal/queue/queue_test.go @@ -0,0 +1 @@ +package queue_test