new file: linear/queue.go
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package linear
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
// Queue []Type{} -> creates a Queue with no fixed size
|
||||
// Implemented as slice
|
||||
type Queue[T any] struct {
|
||||
capacity uint8
|
||||
container []T
|
||||
}
|
||||
|
||||
// QueueFixed -> QueueFixed[Type](size) creates a Queue with no fixed size
|
||||
func QueueFixed[T any](value uint8) *Queue[T] {
|
||||
return &Queue[T]{
|
||||
capacity: value,
|
||||
container: make([]T, 0, value),
|
||||
}
|
||||
}
|
||||
|
||||
// Add() -> enqeues an item, returns err if queue if full
|
||||
func (q *Queue[T]) Add(item T) error {
|
||||
if q.capacity > 0 && q.capacity <= uint8(len(q.container)) {
|
||||
return errors.New("Queue full")
|
||||
}
|
||||
|
||||
q.container = append(q.container, item)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Peek() -> returns the upcoming item
|
||||
func (q *Queue[T]) Peek() (T, bool) {
|
||||
if len(q.container) <= 0 {
|
||||
var zero T
|
||||
return zero, false
|
||||
}
|
||||
|
||||
return q.container[0], true
|
||||
}
|
||||
|
||||
// Pull() -> returns the upcoming item and removes it from Queue
|
||||
func (q *Queue[T]) Pull() (T, error) {
|
||||
if len(q.container) <= 0 {
|
||||
var zero T
|
||||
return zero, errors.New("Queue is empty")
|
||||
}
|
||||
|
||||
upcoming := q.container[0]
|
||||
q.container = q.container[1:]
|
||||
|
||||
return upcoming, nil
|
||||
}
|
||||
|
||||
// Cull() -> removes the last item , returns it and err
|
||||
func (q *Queue[T]) Cull() (T, error) {
|
||||
if len(q.container) <= 0 {
|
||||
var zero T
|
||||
return zero, errors.New("Queue is empty")
|
||||
}
|
||||
|
||||
last := q.container[len(q.container)-1]
|
||||
q.container = q.container[0 : len(q.container)-1]
|
||||
|
||||
return last, nil
|
||||
}
|
||||
|
||||
// Size() -> returns size of queue
|
||||
func (q *Queue[T]) Size() int {
|
||||
return len(q.container)
|
||||
}
|
||||
Reference in New Issue
Block a user