Skip to content

Commit

Permalink
feat: queue
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinZonda committed Nov 19, 2022
1 parent 8ee64ab commit 03cd594
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pkg/typex/queue.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 03cd594

Please sign in to comment.