aoc2023/day-5/queue.go
2024-01-01 11:32:43 +00:00

52 lines
1.0 KiB
Go

package main
import (
log "github.com/sirupsen/logrus"
)
// queue represents a FIFO queue of bounds
type queue struct {
size int
elements []bound
}
// newQueue returns a queue type
// it initializes the queue size and elements
func newQueue(size int, elems []bound) queue {
return queue{size: size, elements: elems}
}
// enqueue adds an item to the queue
func (q *queue) enqueue(elem bound) {
if q.size >= 0 && q.getLength() == q.size {
log.Info("Queue Overflow")
return
}
q.elements = append(q.elements, elem)
}
// dequeue pops an element from the start of the queue
func (q *queue) dequeue() bound {
if q.isEmpty() {
log.Info("Queue UnderFlow")
return bound{}
}
element := q.elements[0]
if q.getLength() == 1 {
q.elements = nil
return element
}
q.elements = q.elements[1:]
return element
}
// getLength returns the number of items in the queue
func (q *queue) getLength() int {
return len(q.elements)
}
// isEmpty returns true if no items are in the queue
func (q *queue) isEmpty() bool {
return len(q.elements) == 0
}