collection

package
v0.0.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2025 License: MIT Imports: 0 Imported by: 0

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

func NewQueue[T any]() *Queue[T]

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

func NewQueueWithCapacity[T any](capacity int) *Queue[T]

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]) Cap added in v0.0.11

func (q *Queue[T]) Cap() int

Cap returns the capacity of 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]) IsEmpty added in v0.0.11

func (q *Queue[T]) IsEmpty() bool

IsEmpty returns true if the queue is empty.

func (*Queue[T]) Len added in v0.0.11

func (q *Queue[T]) Len() int

Len returns the number of elements in the queue.

func (*Queue[T]) Peek added in v0.0.11

func (q *Queue[T]) Peek() (T, bool)

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

func (q *Queue[T]) Remove() (T, bool)

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

func (Set[T]) Add

func (s Set[T]) Add(element T)

Add adds an element to the set.

func (Set[T]) Contains

func (s Set[T]) Contains(element T) bool

Contains returns true if the set contains the element.

func (Set[T]) Remove

func (s Set[T]) Remove(element T)

Remove removes an element from the set.

type Stack

type Stack[T any] struct {
	// contains filtered or unexported fields
}

Stack is a generic non thread-safe LIFO stack implementation.

func NewStack

func NewStack[T any]() *Stack[T]

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

func NewStackWithCapacity[T any](capacity int) *Stack[T]

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]) Cap

func (s *Stack[T]) Cap() int

Cap returns the capacity of the stack.

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]) IsEmpty

func (s *Stack[T]) IsEmpty() bool

IsEmpty returns true if the stack is empty.

func (*Stack[T]) Len

func (s *Stack[T]) Len() int

Len returns the number of elements in the stack.

func (*Stack[T]) Peek

func (s *Stack[T]) Peek() (T, bool)

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

func (s *Stack[T]) Pop() (T, bool)

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

func (*Stack[T]) Push

func (s *Stack[T]) Push(element T)

Push adds an element to the top of the stack.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL