ds

package
v0.0.0-...-878157d Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2025 License: MIT Imports: 3 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GrowAdd

func GrowAdd[K any](slice []K, idx int, val K) []K

Safely adds the value at the slice index provided

func SafeGet

func SafeGet[K any](slice []K, idx int) (K, bool)

Safely gets and returns a value from a slice, and a boolen to indicate boundscheck

Types

type ArrayConstraint

type ArrayConstraint[T any] interface {
	[1]T | [2]T | [3]T | [4]T | [5]T | [6]T | [7]T | [8]T | [9]T | [10]T | [11]T | [12]T | [13]T | [14]T | [15]T | [16]T
}

type ArrayMap

type ArrayMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Acts like a map, but is backed by an array. Can provide better iteration speed at the cost of slower lookups

func NewArrayMap

func NewArrayMap[K comparable, V any]() ArrayMap[K, V]

func (*ArrayMap[K, V]) All

func (m *ArrayMap[K, V]) All() iter.Seq2[K, V]

Iterate through the entire slice

func (*ArrayMap[K, V]) Clear

func (m *ArrayMap[K, V]) Clear()

Clear the entire slice

func (*ArrayMap[K, V]) Delete

func (m *ArrayMap[K, V]) Delete(key K)

Delete a specific index. Note this will move the last index into the hole

func (*ArrayMap[K, V]) Get

func (m *ArrayMap[K, V]) Get(key K) (V, bool)

func (*ArrayMap[K, V]) Put

func (m *ArrayMap[K, V]) Put(key K, val V)

type IndexMap

type IndexMap[K constraints.Integer, V any] struct {
	// contains filtered or unexported fields
}

Acts like a map, but backed by an integer indexed slice instead of a map This is mostly for use cases where you have a small list of increasing numbers that you want to store in an array, but you dont want to worry about ensuring the bounds are always correct Note: If you put a huge key in here, the slice will allocate a ton of space.

func NewIndexMap

func NewIndexMap[K constraints.Integer, V any]() IndexMap[K, V]

func (*IndexMap[K, V]) All

func (m *IndexMap[K, V]) All() iter.Seq2[K, V]

Iterate through the entire slice

func (*IndexMap[K, V]) Clear

func (m *IndexMap[K, V]) Clear()

Clear the entire slice

func (*IndexMap[K, V]) Delete

func (m *IndexMap[K, V]) Delete(idx K)

Delete a specific index

func (*IndexMap[K, V]) Get

func (m *IndexMap[K, V]) Get(idx K) (V, bool)

func (*IndexMap[K, V]) Put

func (m *IndexMap[K, V]) Put(idx K, val V)

type Item

type Item[T any] struct {
	Priority int // The priority of the item in the queue.

	Value T // The value of the item; arbitrary.
	// contains filtered or unexported fields
}

An Item is something we manage in a priority queue.

func NewItem

func NewItem[T any](value T, priority int) *Item[T]

type MiniSlice

type MiniSlice[A ArrayConstraint[T], T comparable] struct {
	Array A
	Slice []T
	// contains filtered or unexported fields
}

A mini slice that allocates the first N elements into an array and then heap allocates the remaining into a traditional slice

func (*MiniSlice[A, T]) All

func (s *MiniSlice[A, T]) All() iter.Seq2[int, T]

Iterates the slice from 0 to the last element added

func (*MiniSlice[A, T]) Append

func (s *MiniSlice[A, T]) Append(val T)

func (*MiniSlice[A, T]) Clear

func (s *MiniSlice[A, T]) Clear()

Clears the array, so the next append will start at index 0

func (*MiniSlice[A, T]) Delete

func (s *MiniSlice[A, T]) Delete(idx int)

Removes the element at the supplied index, swapping the element that was at the last index to the supplied index

func (*MiniSlice[A, T]) Find

func (s *MiniSlice[A, T]) Find(searchVal T) int

Find and return the index of the first element, else return -1

func (*MiniSlice[A, T]) Get

func (s *MiniSlice[A, T]) Get(idx int) T

func (*MiniSlice[A, T]) Len

func (s *MiniSlice[A, T]) Len() int

Returns the number of elements in the slice

func (*MiniSlice[A, T]) Set

func (s *MiniSlice[A, T]) Set(idx int, val T)

func (*MiniSlice[A, T]) SliceLast

func (s *MiniSlice[A, T]) SliceLast()

Slices the last element

type PriorityMap

type PriorityMap[K comparable, T any] struct {
	// contains filtered or unexported fields
}

Note: Higher value is pulled out first

func NewPriorityMap

func NewPriorityMap[K comparable, T any]() *PriorityMap[K, T]

func (*PriorityMap[K, T]) Clear

func (q *PriorityMap[K, T]) Clear()

Removes all items from the queue

func (*PriorityMap[K, T]) Get

func (q *PriorityMap[K, T]) Get(key K) (T, int, bool)

Gets the item, doesnt remove it

func (*PriorityMap[K, T]) Len

func (q *PriorityMap[K, T]) Len() int

func (*PriorityMap[K, T]) Pop

func (q *PriorityMap[K, T]) Pop() (K, T, int, bool)

Pops the highest priority item

func (*PriorityMap[K, T]) Put

func (q *PriorityMap[K, T]) Put(key K, val T, priority int)

Adds the item, overwriting the old one if needed

func (*PriorityMap[K, T]) Remove

func (q *PriorityMap[K, T]) Remove(key K) (T, bool)

Gets the value and removes it from the queue

type PriorityQueue

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

func NewPriorityQueue

func NewPriorityQueue[T any]() *PriorityQueue[T]

Note: Higher value is pulled out first

func (*PriorityQueue[T]) Clear

func (pq *PriorityQueue[T]) Clear()

func (*PriorityQueue[T]) Len

func (pq *PriorityQueue[T]) Len() int

func (*PriorityQueue[T]) Pop

func (pq *PriorityQueue[T]) Pop() *Item[T]

func (*PriorityQueue[T]) Push

func (pq *PriorityQueue[T]) Push(item *Item[T])

func (*PriorityQueue[T]) Remove

func (pq *PriorityQueue[T]) Remove(item *Item[T])

func (*PriorityQueue[T]) Update

func (pq *PriorityQueue[T]) Update(item *Item[T])

type Queue

type Queue[T any] struct {
	Buffer   []T
	ReadIdx  int
	WriteIdx int
	// contains filtered or unexported fields
}

func NewFixedQueue

func NewFixedQueue[T any](length int) *Queue[T]

func NewQueue

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

func (*Queue[T]) Add

func (q *Queue[T]) Add(t T)

func (*Queue[T]) GrowDouble

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

func (*Queue[T]) Len

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

func (*Queue[T]) Peek

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

func (*Queue[T]) PeekLast

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

func (*Queue[T]) Remove

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

type RingBuffer

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

func NewRingBuffer

func NewRingBuffer[T any](length int) *RingBuffer[T]

func (*RingBuffer[T]) Add

func (b *RingBuffer[T]) Add(t T)

func (*RingBuffer[T]) Buffer

func (b *RingBuffer[T]) Buffer() []T

TODO - Maybe convert this to an iterator

func (*RingBuffer[T]) Cap

func (b *RingBuffer[T]) Cap() int

func (*RingBuffer[T]) Len

func (b *RingBuffer[T]) Len() int

func (*RingBuffer[T]) Remove

func (b *RingBuffer[T]) Remove() (T, bool)

Returns the last element and false if the buffer is emptied

type Stack

type Stack[T any] struct {
	Buffer []T
}

func NewStack

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

func (*Stack[T]) Add

func (s *Stack[T]) Add(t T)

func (*Stack[T]) Len

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

func (*Stack[T]) Remove

func (s *Stack[T]) Remove() (T, bool)
func (s *Stack[T]) Peek() (T, bool) {
	if q.ReadIdx == q.WriteIdx {
		var ret T
		return ret, false
	}
	return q.Buffer[q.ReadIdx], true
}
func (s *Stack[T]) PeekLast() (T, bool) {
	if q.ReadIdx == q.WriteIdx {
		var ret T
		return ret, false
	}
	idx := (q.WriteIdx + len(q.Buffer) - 1) % len(q.Buffer)
	return q.Buffer[idx], true
}

Jump to

Keyboard shortcuts

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