Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Queue ¶
type Queue[T any] interface { // Len returns length of Queue Len() int // Push pushes element to end of Queue Push(v T) // Pop pops element from head of Queue Pop() (T, bool) // Head returns head element of Queue Head() (T, bool) // Tail returns tail element of Queue Tail() (T, bool) // Range calls f sequentially for each element present in the Queue. // If f returns false, range stops the iteration. Range(f func(T) bool) // Clear releases all the entries, resulting in an empty Queue. Clear() }
Example ¶
package main
import (
"fmt"
"github.com/xoctopus/x/container/queue"
)
func main() {
for _, q := range []queue.Queue[any]{
queue.NewSafeQueue[any](),
queue.NewQueue[any](),
} {
q.Push(1)
q.Push(2)
q.Push(3)
q.Push(4)
head, _ := q.Head()
tail, _ := q.Tail()
fmt.Println("head:", head)
fmt.Println("tail:", tail)
v, _ := q.Pop()
fmt.Println(v)
for v = range q.Range {
fmt.Println(v)
continue
}
q.Clear()
q.Len()
_, ok := q.Head()
fmt.Println("head:", ok)
_, ok = q.Tail()
fmt.Println("tail:", ok)
_, ok = q.Pop()
fmt.Println("pop: ", ok)
}
}
Output: head: 1 tail: 4 1 2 3 4 head: false tail: false pop: false head: 1 tail: 4 1 2 3 4 head: false tail: false pop: false
func NewSafeQueue ¶
Click to show internal directories.
Click to hide internal directories.