aocutils

package module
v0.0.0-...-d5bb19f Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2022 License: MIT Imports: 1 Imported by: 0

README

advent of code helper utilities.

Documentation

Overview

Package aocutils is a collection of utilities meant for helping with advent of code. Might be otherwise useful.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[T constraints.Signed | constraints.Float](a T) T

func Delta

func Delta[T constraints.Integer | constraints.Float](a, b T) T

func MapKeys

func MapKeys[T comparable, Y any](m map[T]Y) []T

func Max

func Max[T constraints.Ordered](t ...T) T

func Min

func Min[T constraints.Ordered](t ...T) T

func SliceMap

func SliceMap[T any](vals []T, f func(T) T) []T

SliceMap - apply a function f to all elements of slice vals, returns the resulting slice.

func SliceMapMutate

func SliceMapMutate[T1 any, T2 any](vals []T1, f func(T1) T2) []T2

func SliceMapNoModify

func SliceMapNoModify[T any](vals []T, f func(T) T) []T

SliceMapNoModify - like SliceMap but does not modify the vals slice.

Types

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set is a trivial set implementation based on maps.

func MakeSet

func MakeSet[T comparable](data []T) *Set[T]

MakeSet - factory method. Returns a set containing the data.

func (*Set[T]) Add

func (s *Set[T]) Add(ts ...T) *Set[T]

Add one or more elements to the set. Returns a reference to the set.

func (*Set[T]) Delete

func (s *Set[T]) Delete(ts ...T) *Set[T]

Delete one or more elements from set. Returns the set.

func (*Set[T]) InSet

func (s *Set[T]) InSet(t T) bool

InSet returns true if t is in the set, false otherwise.

func (*Set[T]) Len

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

Len returns length of set.

func (*Set[T]) Map

func (s *Set[T]) Map() map[T]bool

Map is a getter function for the internal map.

func (*Set[T]) UnorderedSlice

func (s *Set[T]) UnorderedSlice() []T

UnorderedSlice returns an unordered slice containing the elements in the set.

type Stack

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

Stack implements a stack using generics.

func NewStack

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

NewStack is a trivial factory method.

func (*Stack[T]) Clone

func (s *Stack[T]) Clone() *Stack[T]

Clone returns a new stack with a fresh copy of the internal slice

func (*Stack[T]) Len

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

Len returns the length of the internal slice.

func (*Stack[T]) Peek

func (s *Stack[T]) Peek(i int) T

Peek gives us the value at index i and returns it. If index i does not exist it returns the zero value of type T. negatives count from the right hand side, so -1 gives the last element.

func (*Stack[T]) Poke

func (s *Stack[T]) Poke(i int, t ...T) *Stack[T]

Poke - poke values in at index i. If i is invalid, returns nil. Otherwise inserts t into stack at index i, returning a reference to the stack. Negative indexes count from the right, however -1 will insert one before the last element of the stack. To insert at the end, either use Push() or passive a positive integer equal to the length of the stack.

func (*Stack[T]) Pop

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

Pop - pop the last value from the stack and return it. If the stack is empty, return the zero value of T

func (*Stack[T]) PopN

func (s *Stack[T]) PopN(n int) []T

PopN - pop up to n values off the stack and return them. If there are less values in the stack than n, the length of the return will be the number of values in the stack.

func (*Stack[T]) Push

func (s *Stack[T]) Push(t ...T) *Stack[T]

Push - pushone or more values onto the stack. Returns a reference to the stack.

func (*Stack[T]) Set

func (s *Stack[T]) Set(i int, t T) *Stack[T]

Set assigns value t at index i. negative values of i are allowed, counting from right. If i is invalid, return value is nil, otherwise stack is returned.

func (*Stack[T]) SetVals

func (s *Stack[T]) SetVals(vals []T) *Stack[T]

SetVals sets the values of the internal slice and returns a reference to the stack s. No copy is made, just an assignment.

func (*Stack[T]) Shift

func (s *Stack[T]) Shift() T

Shift removes the front most element from the stack and returns it. If the stack is empty, returns the zero value of type T.

func (*Stack[T]) ShiftN

func (s *Stack[T]) ShiftN(n int) []T

ShiftN removes up to n elements from the front of the stack and returns them. If n is larger than the size of the internal slice, the length of the return will be smaller than n.

func (*Stack[T]) Unshift

func (s *Stack[T]) Unshift(t ...T) *Stack[T]

Unshift moves one or more elements t onto the front of the stack. Returns a reference to the stack.

func (*Stack[T]) Vals

func (s *Stack[T]) Vals() []T

Vals - getter function for the values in the stack.

type Window

type Window[T comparable] struct {
	// contains filtered or unexported fields
}

Window is meant for sliding window loops where you want to maintain statistics about how many occurrences of a given value of type T are in a subset of the data.

func NewWindow

func NewWindow[T comparable](data []T) *Window[T]

NewWindow - Factory method for Window. Initializes state with data, returns a Window

func (*Window[T]) Len

func (w *Window[T]) Len() int

Len - length of the data set inside of the window.

func (*Window[T]) Map

func (w *Window[T]) Map() map[T]int

Map - getter function for the internal map.

func (*Window[T]) Slide

func (w *Window[T]) Slide(remove, add T) *Window[T]

Slide moves the window down. remove is the value being passed out of the window, add is the value passing into the window.

func (*Window[T]) UnorderedSlice

func (w *Window[T]) UnorderedSlice() []T

UnorderedSlice - this returns the data inside the internal map, unordered.

Jump to

Keyboard shortcuts

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