diff --git a/pkg/typex/queue.go b/pkg/typex/queue.go new file mode 100644 index 0000000..e059ff9 --- /dev/null +++ b/pkg/typex/queue.go @@ -0,0 +1,29 @@ +package typex + +type Queue[T any] struct { + data []T +} + +func (q *Queue[T]) Enqueue(data T) { + q.data = append(q.data, data) +} + +func (q *Queue[T]) Dequeue() (data T, ok bool) { + data, ok = q.TakeFirst() + if ok { + q.data = q.data[1:] + } + return +} + +func (q *Queue[T]) TakeFirst() (data T, ok bool) { + if len(q.data) == 0 { + return nil, false + } + data = q.data[0] + return data, true +} + +func (q *Queue[T]) ToSlice() []T { + return q.data +}