Documentation
¶
Overview ¶
Package chequeue provides a generic Queue implementation for Go.
Queue is a FIFO (First-In-First-Out) data structure that provides O(1) amortized performance for Enqueue and Dequeue operations.
Basic Usage ¶
queue := chequeue.New[int]() queue.Enqueue(1) queue.Enqueue(2) value, ok := queue.Dequeue() // 1, true fmt.Println(queue.Size()) // 1
Thread Safety ¶
Queue is not thread-safe. For concurrent use, external synchronization is required (e.g., using sync.Mutex).
Performance ¶
Enqueue and Dequeue operations have O(1) amortized time complexity. The implementation uses a dynamic slice with automatic resizing.
Example ¶
Example demonstrates basic Queue operations
package main
import (
"fmt"
"github.com/comfortablynumb/che/pkg/chequeue"
)
func main() {
queue := chequeue.New[int]()
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
// FIFO order
val, _ := queue.Dequeue()
fmt.Println("First out:", val)
val, _ = queue.Dequeue()
fmt.Println("Second out:", val)
fmt.Println("Size:", queue.Size())
}
Output: First out: 1 Second out: 2 Size: 1
Index ¶
- type Queue
- func (q *Queue[T]) Clear()
- func (q *Queue[T]) Clone() *Queue[T]
- func (q *Queue[T]) Contains(item T, equals func(a, b T) bool) bool
- func (q *Queue[T]) Dequeue() (T, bool)
- func (q *Queue[T]) Enqueue(item T)
- func (q *Queue[T]) EnqueueMultiple(items ...T)
- func (q *Queue[T]) ForEach(fn func(item T) bool)
- func (q *Queue[T]) IsEmpty() bool
- func (q *Queue[T]) Peek() (T, bool)
- func (q *Queue[T]) Size() int
- func (q *Queue[T]) String() string
- func (q *Queue[T]) ToSlice() []T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Queue ¶
type Queue[T any] struct { // contains filtered or unexported fields }
Queue is a generic FIFO (First-In-First-Out) queue implementation. It provides O(1) amortized time complexity for Enqueue and Dequeue operations. Queue is not thread-safe. For concurrent use, external synchronization is required.
Example (Bfs) ¶
ExampleQueue_bfs demonstrates using Queue for breadth-first search
package main
import (
"fmt"
"github.com/comfortablynumb/che/pkg/chequeue"
)
func main() {
type Node struct {
Value int
Children []int
}
// Simple tree: 1 -> [2, 3], 2 -> [4, 5]
nodes := map[int]Node{
1: {Value: 1, Children: []int{2, 3}},
2: {Value: 2, Children: []int{4, 5}},
3: {Value: 3, Children: []int{}},
4: {Value: 4, Children: []int{}},
5: {Value: 5, Children: []int{}},
}
queue := chequeue.New[int]()
queue.Enqueue(1) // Start with root
for !queue.IsEmpty() {
nodeID, _ := queue.Dequeue()
node := nodes[nodeID]
fmt.Println(node.Value)
for _, childID := range node.Children {
queue.Enqueue(childID)
}
}
}
Output: 1 2 3 4 5
Example (TaskQueue) ¶
ExampleQueue_taskQueue demonstrates using Queue for task processing
package main
import (
"fmt"
"github.com/comfortablynumb/che/pkg/chequeue"
)
func main() {
type Task struct {
ID int
Name string
}
taskQueue := chequeue.New[Task]()
// Add tasks
taskQueue.Enqueue(Task{ID: 1, Name: "Process data"})
taskQueue.Enqueue(Task{ID: 2, Name: "Send email"})
taskQueue.Enqueue(Task{ID: 3, Name: "Update database"})
// Process tasks in FIFO order
for !taskQueue.IsEmpty() {
task, _ := taskQueue.Dequeue()
fmt.Printf("Processing task %d: %s\n", task.ID, task.Name)
}
}
Output: Processing task 1: Process data Processing task 2: Send email Processing task 3: Update database
func New ¶
New creates and returns a new empty Queue.
Example ¶
ExampleNew demonstrates creating a new queue
package main
import (
"fmt"
"github.com/comfortablynumb/che/pkg/chequeue"
)
func main() {
queue := chequeue.New[string]()
queue.Enqueue("hello")
queue.Enqueue("world")
fmt.Println("Size:", queue.Size())
}
Output: Size: 2
func NewFromSlice ¶
NewFromSlice creates and returns a new Queue containing all elements from the given slice. Elements are enqueued in the order they appear in the slice.
Example ¶
ExampleNewFromSlice demonstrates creating a queue from a slice
package main
import (
"fmt"
"github.com/comfortablynumb/che/pkg/chequeue"
)
func main() {
slice := []int{1, 2, 3, 4, 5}
queue := chequeue.NewFromSlice(slice)
// First element from slice is at front
val, _ := queue.Dequeue()
fmt.Println("First:", val)
}
Output: First: 1
func NewWithCapacity ¶
NewWithCapacity creates and returns a new empty Queue with the specified initial capacity. This can improve performance when the expected size is known in advance.
func (*Queue[T]) Clone ¶
Clone returns a shallow copy of the queue.
Example ¶
ExampleQueue_Clone demonstrates cloning a queue
package main
import (
"fmt"
"github.com/comfortablynumb/che/pkg/chequeue"
)
func main() {
original := chequeue.NewFromSlice([]int{1, 2, 3})
clone := original.Clone()
clone.Enqueue(4)
fmt.Println("Original size:", original.Size())
fmt.Println("Clone size:", clone.Size())
}
Output: Original size: 3 Clone size: 4
func (*Queue[T]) Contains ¶
Contains checks if an element exists in the queue. This operation is O(n) as it requires scanning all elements.
func (*Queue[T]) Dequeue ¶
Dequeue removes and returns the element at the front of the queue. Returns the element and true if successful, or zero value and false if the queue is empty.
Example ¶
ExampleQueue_Dequeue demonstrates removing elements
package main
import (
"fmt"
"github.com/comfortablynumb/che/pkg/chequeue"
)
func main() {
queue := chequeue.NewFromSlice([]int{1, 2, 3})
val, ok := queue.Dequeue()
fmt.Println("Value:", val, "Success:", ok)
val, _ = queue.Dequeue()
fmt.Println("Next:", val)
}
Output: Value: 1 Success: true Next: 2
func (*Queue[T]) Enqueue ¶
func (q *Queue[T]) Enqueue(item T)
Enqueue adds an element to the back of the queue.
Example ¶
ExampleQueue_Enqueue demonstrates adding elements
package main
import (
"fmt"
"github.com/comfortablynumb/che/pkg/chequeue"
)
func main() {
queue := chequeue.New[int]()
queue.Enqueue(10)
queue.Enqueue(20)
queue.Enqueue(30)
fmt.Println("Size:", queue.Size())
}
Output: Size: 3
func (*Queue[T]) EnqueueMultiple ¶
func (q *Queue[T]) EnqueueMultiple(items ...T)
EnqueueMultiple adds multiple elements to the back of the queue.
func (*Queue[T]) ForEach ¶
ForEach executes a function for each element in the queue in FIFO order. If the function returns false, iteration stops.
Example ¶
ExampleQueue_ForEach demonstrates iterating over elements
package main
import (
"fmt"
"github.com/comfortablynumb/che/pkg/chequeue"
)
func main() {
queue := chequeue.NewFromSlice([]string{"a", "b", "c"})
queue.ForEach(func(item string) bool {
fmt.Println(item)
return true
})
}
Output: a b c
func (*Queue[T]) Peek ¶
Peek returns the element at the front of the queue without removing it. Returns the element and true if successful, or zero value and false if the queue is empty.
Example ¶
ExampleQueue_Peek demonstrates peeking at the front
package main
import (
"fmt"
"github.com/comfortablynumb/che/pkg/chequeue"
)
func main() {
queue := chequeue.NewFromSlice([]int{100, 200, 300})
// Peek doesn't remove the element
val, ok := queue.Peek()
fmt.Println("Front:", val, "Success:", ok)
fmt.Println("Size still:", queue.Size())
}
Output: Front: 100 Success: true Size still: 3
func (*Queue[T]) ToSlice ¶
func (q *Queue[T]) ToSlice() []T
ToSlice returns a slice containing all elements in FIFO order.
Example ¶
ExampleQueue_ToSlice demonstrates converting queue to slice
package main
import (
"fmt"
"github.com/comfortablynumb/che/pkg/chequeue"
)
func main() {
queue := chequeue.NewFromSlice([]int{10, 20, 30})
slice := queue.ToSlice()
fmt.Println("Slice:", slice)
}
Output: Slice: [10 20 30]