Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Queue ¶ added in v0.0.11
type Queue[T any] struct { // contains filtered or unexported fields }
Queue is a generic non thread-safe unbounded FIFO queue implementation.
func NewQueue ¶ added in v0.0.11
NewQueue creates a new queue.
Example ¶
queue := NewQueue[int]() fmt.Println("Is empty:", queue.IsEmpty()) fmt.Println("Len:", queue.Len()) fmt.Println("Cap:", queue.Cap()) _, peekOK := queue.Peek() fmt.Println("Peek OK:", peekOK) _, removeOK := queue.Remove() fmt.Println("Remove OK:", removeOK)
Output: Is empty: true Len: 0 Cap: 0 Peek OK: false Remove OK: false
func NewQueueWithCapacity ¶ added in v0.0.11
NewQueueWithCapacity creates a new queue with the specified initial capacity.
Example ¶
queue := NewQueueWithCapacity[int](10) fmt.Println("Is empty:", queue.IsEmpty()) fmt.Println("Len:", queue.Len()) fmt.Println("Cap:", queue.Cap()) _, peekOk := queue.Peek() fmt.Println("Peek OK:", peekOk) _, removeOk := queue.Remove() fmt.Println("Remove OK:", removeOk)
Output: Is empty: true Len: 0 Cap: 10 Peek OK: false Remove OK: false
func (*Queue[T]) Add ¶ added in v0.0.11
func (q *Queue[T]) Add(element T)
Add adds an element to the queue.
func (*Queue[T]) Clear ¶ added in v0.0.11
func (q *Queue[T]) Clear()
Clear removes all elements from the queue.
Example ¶
{ queue := NewQueue[int]() queue.Add(1) queue.Add(2) queue.Add(3) fmt.Println("Len before clear:", queue.Len()) capBeforeClear := queue.Cap() queue.Clear() fmt.Println("Len after clear:", queue.Len()) fmt.Println("Cap unchanged:", queue.Cap() == capBeforeClear) } queue := NewQueueWithCapacity[int](10) queue.Add(1) queue.Add(2) queue.Add(3) queue.Clear() fmt.Println("Cap after clear:", queue.Cap())
Output: Len before clear: 3 Len after clear: 0 Cap unchanged: true Cap after clear: 10
func (*Queue[T]) Peek ¶ added in v0.0.11
Peek returns the next element to be removed from the queue (without removing it) and a boolean indicating if the operation was successful.
Example ¶
queue := NewQueue[int]() queue.Add(1) value, ok := queue.Peek() fmt.Println("Value:", value) fmt.Println("OK:", ok) fmt.Println("Len:", queue.Len())
Output: Value: 1 OK: true Len: 1
func (*Queue[T]) Remove ¶ added in v0.0.11
Remove removes and returns the next element of the queue and a boolean indicating if the operation was successful.
Example ¶
queue := NewQueue[int]() queue.Add(1) value, removeOk := queue.Remove() fmt.Println("Value:", value) fmt.Println("Remove OK:", removeOk) fmt.Println("Len:", queue.Len()) _, peekOk := queue.Peek() fmt.Println("Peek OK:", peekOk)
Output: Value: 1 Remove OK: true Len: 0 Peek OK: false
type Set ¶
type Set[T comparable] map[T]struct{}
Set is a generic non thread-safe set implementation.
Example ¶
set := make(Set[int]) set.Add(1) set.Add(2) set.Add(3) fmt.Println(set.Contains(1)) fmt.Println(set.Contains(4)) set.Remove(2) fmt.Println(set.Contains(2)) fmt.Println(len(set))
Output: true false false 2
type Stack ¶
type Stack[T any] struct { // contains filtered or unexported fields }
Stack is a generic non thread-safe LIFO stack implementation.
func NewStack ¶
NewStack creates a new stack.
Example ¶
stack := NewStack[int]() fmt.Println("Is empty:", stack.IsEmpty()) fmt.Println("Len:", stack.Len()) fmt.Println("Cap:", stack.Cap()) _, peekOK := stack.Peek() fmt.Println("Peek OK:", peekOK) _, popOK := stack.Pop() fmt.Println("Pop OK:", popOK)
Output: Is empty: true Len: 0 Cap: 0 Peek OK: false Pop OK: false
func NewStackWithCapacity ¶
NewStackWithCapacity creates a new stack with the specified initial capacity.
Example ¶
stack := NewStackWithCapacity[int](10) fmt.Println("Is empty:", stack.IsEmpty()) fmt.Println("Len:", stack.Len()) fmt.Println("Cap:", stack.Cap()) _, peekOK := stack.Peek() fmt.Println("Peek OK:", peekOK) _, popOK := stack.Pop() fmt.Println("Pop OK:", popOK)
Output: Is empty: true Len: 0 Cap: 10 Peek OK: false Pop OK: false
func (*Stack[T]) Clear ¶
func (s *Stack[T]) Clear()
Clear removes all elements from the stack.
Example ¶
{ stack := NewStack[int]() stack.Push(1) stack.Push(2) stack.Push(3) fmt.Println("Len before clear:", stack.Len()) capBeforeClear := stack.Cap() stack.Clear() fmt.Println("Len after clear:", stack.Len()) fmt.Println("Cap unchanged:", stack.Cap() == capBeforeClear) } stack := NewStackWithCapacity[int](10) stack.Push(1) stack.Push(2) stack.Push(3) stack.Clear() fmt.Println("Cap after clear:", stack.Cap())
Output: Len before clear: 3 Len after clear: 0 Cap unchanged: true Cap after clear: 10
func (*Stack[T]) Peek ¶
Peek returns the top element of the stack (without removing it) and a boolean indicating if the operation was successful.
Example ¶
stack := NewStack[int]() stack.Push(1) top, ok := stack.Peek() fmt.Println("Top:", top) fmt.Println("OK:", ok) fmt.Println("Len:", stack.Len())
Output: Top: 1 OK: true Len: 1
func (*Stack[T]) Pop ¶
Pop removes and returns the top element of the stack and a boolean indicating if the operation was successful.
Example ¶
stack := NewStack[int]() stack.Push(1) top, popOk := stack.Pop() fmt.Println("Top:", top) fmt.Println("Pop OK:", popOk) fmt.Println("Len:", stack.Len()) _, peekOk := stack.Peek() fmt.Println("Peek OK:", peekOk)
Output: Top: 1 Pop OK: true Len: 0 Peek OK: false