collections

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package collections contains custom collection types.

Package collections contains custom collection types.

Package collections contains custom collection types.

Package collections contains custom collection types.

Package collections contains custom collection types.

Package collections contains custom collection types.

Package collections contains custom collection types.

Package collections contains custom collection types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MakeGrid

func MakeGrid[T any](rows, cols int) [][]T

MakeGrid creates a fully allocated 2d slice of a given type and size.

Types

type LinkedList

type LinkedList[T any] struct {
	Head *ListNode[T]
	Tail *ListNode[T]
}

LinkedList is a doubly linked list.

func NewLinkedList

func NewLinkedList[T any]() *LinkedList[T]

NewLinkedList creates a new linked list.

func (*LinkedList[T]) Add

func (l *LinkedList[T]) Add(value T)

Add adds an element to the linked list. This element will be added to the top of the list for speed.

func (*LinkedList[T]) Append

func (l *LinkedList[T]) Append(value T)

Append adds an element to the linked list. This element will be added to the end of the list.

func (*LinkedList[T]) GetValueAt

func (l *LinkedList[T]) GetValueAt(index int) (T, error)

GetValueAt returns the value in the node at the specified index.

func (*LinkedList[T]) Insert

func (l *LinkedList[T]) Insert(value T, index int) error

Insert adds an element to the linked list. This element will be added at the specified index.

type List

type List[T cmp.Ordered] struct {
	// contains filtered or unexported fields
}

List is a general purpose List class based on slices.

func NewList

func NewList[T cmp.Ordered]() *List[T]

NewList creates a new list.

func (*List[T]) Append

func (l *List[T]) Append(value T)

Append appends a value to the end of the list.

func (*List[T]) Clear

func (l *List[T]) Clear()

Clear removes all elements from the list.

func (*List[T]) Contains

func (l *List[T]) Contains(value T) bool

Contains returns whether or not the list contains the specified value.

func (*List[T]) Filter

func (l *List[T]) Filter(filterFunc func(T) bool) *List[T]

Filter returns a new list with the elements that pass the filter function.

func (*List[T]) Get

func (l *List[T]) Get(index int) (T, error)

Get returns the indexed value from the list.

func (*List[T]) GetFirst

func (l *List[T]) GetFirst() T

GetFirst returns the first element in the list.

func (*List[T]) GetLast

func (l *List[T]) GetLast() T

GetLast returns the last element in the list.

func (*List[T]) GetRandom

func (l *List[T]) GetRandom() T

GetRandom returns a random element from the list.

func (*List[T]) IndexOf

func (l *List[T]) IndexOf(value T) int

IndexOf returns the index of the speified value if it is in the list, or -1 if not.

func (*List[T]) Insert

func (l *List[T]) Insert(index int, value T) error

Insert inserts a value at the given index.

func (*List[T]) Length

func (l *List[T]) Length() int

Length returns the number of elements in the list.

func (*List[T]) Map

func (l *List[T]) Map(mapFunc func(T) T) *List[T]

Map returns a new list with elements obtained by applying the map function to each element.

func (*List[T]) Prepend

func (l *List[T]) Prepend(value T)

Prepend prepends a value to the start of the list.

func (*List[T]) Remove

func (l *List[T]) Remove(index int) (T, error)

Remove removes and returns the indexed value from the list.

func (*List[T]) RemoveFirst

func (l *List[T]) RemoveFirst() T

RemoveFirst removes and returns the first value on the list.

func (*List[T]) RemoveLast

func (l *List[T]) RemoveLast() T

RemoveLast removes and returns the last value on the list.

func (*List[T]) Reverse

func (l *List[T]) Reverse()

Reverse reverses the list.

func (*List[T]) Shuffle

func (l *List[T]) Shuffle()

Shuffle randomly shuffles the list.

func (*List[T]) Slice

func (l *List[T]) Slice() []T

Slice returns a slice containing all the values in this list.

func (*List[T]) Sort

func (l *List[T]) Sort()

Sort sorts the list.

func (*List[T]) String

func (l *List[T]) String() string

String returns a string representation of the list.

type ListNode

type ListNode[T any] struct {
	Value T
	Next  *ListNode[T]
	Prev  *ListNode[T]
}

ListNode represents a single element in a linked list.

func NewListNode

func NewListNode[T any](value T) *ListNode[T]

NewListNode creates a new ListNode.

type MaxPQ

type MaxPQ[T cmp.Ordered] struct {
	// contains filtered or unexported fields
}

MaxPQ is a priority queue that allows getting the maximum value in the queue.

func NewMaxPQ

func NewMaxPQ[T cmp.Ordered]() *MaxPQ[T]

NewMaxPQ creates a new MaxPQ.

func (*MaxPQ[T]) DelMax

func (m *MaxPQ[T]) DelMax() T

DelMax removes and returns the maximum value in the queue.

func (*MaxPQ[T]) Insert

func (m *MaxPQ[T]) Insert(value T)

Insert inserts a new value into the queue.

func (*MaxPQ[T]) IsEmpty

func (m *MaxPQ[T]) IsEmpty() bool

IsEmpty returns whether or not there are any items in the queue.

func (*MaxPQ[T]) Max

func (m *MaxPQ[T]) Max() T

Max returns the maximum value in the queue, but does not remove it.

func (*MaxPQ[T]) Size

func (m *MaxPQ[T]) Size() int

Size returns the number of items in the queue.

func (*MaxPQ[T]) String

func (m *MaxPQ[T]) String() string

type MinPQ

type MinPQ[T cmp.Ordered] struct {
	// contains filtered or unexported fields
}

MinPQ is a priority queue that allows getting the minimum value in the queue.

func NewMinPQ

func NewMinPQ[T cmp.Ordered]() *MinPQ[T]

NewMinPQ creates a new MinPQ.

func (*MinPQ[T]) DelMin

func (m *MinPQ[T]) DelMin() T

DelMin removes and returns the minimum value in the queue.

func (*MinPQ[T]) Insert

func (m *MinPQ[T]) Insert(value T)

Insert inserts a new value into the queue.

func (*MinPQ[T]) IsEmpty

func (m *MinPQ[T]) IsEmpty() bool

IsEmpty returns whether or not there are any items in the queue.

func (*MinPQ[T]) Min

func (m *MinPQ[T]) Min() T

Min returns the minimum value in the queue, but does not remove it.

func (*MinPQ[T]) Size

func (m *MinPQ[T]) Size() int

Size returns the number of items in the queue.

func (*MinPQ[T]) String

func (m *MinPQ[T]) String() string

type Queue

type Queue[T any] []T

Queue is a FIFO queue of items.

func NewQueue

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

NewQueue creates a new queue.

func (*Queue[T]) Dequeue

func (q *Queue[T]) Dequeue() T

Dequeue dequeues the next item on the list.

func (*Queue[T]) Enqueue

func (q *Queue[T]) Enqueue(item T)

Enqueue enqueues a single item.

func (*Queue[T]) EnqueueAll

func (q *Queue[T]) EnqueueAll(items []T)

EnqueueAll enqueues a list of items.

func (Queue[T]) HasItems

func (q Queue[T]) HasItems() bool

HasItems returns whether or not this queue has items. Is the opposite of IsEmpty.

func (Queue[T]) IsEmpty

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

IsEmpty returns whether or not this queue is empty

func (Queue[T]) Size

func (q Queue[T]) Size() int

Size returns the size of the queue.

type Set

type Set[T comparable] map[T]bool

Set is a set of unique items.

func NewSet

func NewSet[T comparable]() Set[T]

NewSet creates a new set.

func (Set[T]) Add

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

Add adds an item to a set.

func (Set[T]) Delete

func (s Set[T]) Delete(item T)

Delete adds an item to a set.

func (Set[T]) Has

func (s Set[T]) Has(item T) bool

Has checks if a set has an item.

func (Set[T]) HasItems

func (s Set[T]) HasItems() bool

HasItems returns whether or not this set has items. Is the opposite of IsEmpty.

func (Set[T]) IsEmpty

func (s Set[T]) IsEmpty() bool

IsEmpty returns whether or not this set is empty.

func (Set[T]) Items

func (s Set[T]) Items() []T

Items returns all the items in this set.

func (Set[T]) Size

func (s Set[T]) Size() int

Size returns the size of this set.

type Stack

type Stack[T any] []T

Stack is a LIFO stack of items.

func NewStack

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

NewStack creates a new stack.

func (Stack[T]) HasItems

func (q Stack[T]) HasItems() bool

HasItems returns whether or not this stack has items. Is the opposite of IsEmpty.

func (Stack[T]) IsEmpty

func (q Stack[T]) IsEmpty() bool

IsEmpty returns whether or not this stack is empty

func (*Stack[T]) Pop

func (q *Stack[T]) Pop() T

Pop pops an item off of the stack.

func (*Stack[T]) Push

func (q *Stack[T]) Push(item T)

Push pushes a single item onto the stack.

func (*Stack[T]) PushAll

func (q *Stack[T]) PushAll(items []T)

PushAll pushes a list of items onto the stack.

func (Stack[T]) Size

func (q Stack[T]) Size() int

Size returns the size of the stack.

Jump to

Keyboard shortcuts

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