Documentation
¶
Overview ¶
This package provides generic helper functions for generic container types, as well as "generic" utility functions (the other meaning of generic).
Index ¶
- func AddExitCleanup(fn func())
- func AllocAppend[T any](list *[]T) *T
- func Append[T any](list *[]T, items ...T) int
- func Assert(cond bool, msg string)
- func CappedLength[T any](slice []T, maxLen int, slack int) []T
- func Clamp[T numeric](start T, item *T, end T)
- func Cleanup()
- func Clone[T any](s []T) []T
- func EnsureMapNotNil[K comparable, V any](m *map[K]V)
- func EnsureSliceNotNil[T any](m *[]T)
- func ExitWithCleanup(code int)
- func GrowSlice[T any](m *[]T, length int)
- func IndexOf[T comparable](list []T, item T) int
- func InitMap[K comparable, V any](m *map[K]V)
- func InitSlice[T any](m *[]T)
- func InsertAt[T any](list *[]T, idx int, items ...T)
- func IntAbs[T int_enum](a T) int
- func IsBetween[T numeric](start, item, end T) bool
- func JSONify(obj any, indent string) string
- func JoinToString[T any](list []T, sep string, stringer func(T) string) string
- func Last[T any](list []T) T
- func LogError(e error)
- func LogWarningf(format string, v ...any)
- func MapEntry[K comparable, V any](m map[K]V, key K, fn func(k K) V) V
- func Max[T numeric](a T, b T) T
- func Min[T numeric](a T, b T) T
- func MulDiv[T Inty](a, b, c T) T
- func MulF[T Inty, F Floaty](n T, f F) T
- func Must[T any](value T, err error) T
- func MustNotNil[T any](p *T) *T
- func MustOK(err error)
- func MustTrue(b bool, msg error)
- func OneOf[T comparable](item T, list []T) bool
- func ReadFromJSONFile[T any](filepath string, obj *T) error
- func RemoveAt[T any](list *[]T, idx int, count int)
- func Reset[T any](ptr *T)
- func ResetSlice[T any](list *[]T)
- func Reverse[T any](a []T)
- func SetsEqual[T comparable](s1 *Set[T], s2 *Set[T]) bool
- func SetupSigTermCleanup()
- func SharedPrefix(str1, str2 string) string
- func SharedSuffix(str1, str2 string) string
- func ShrinkTo[T any](list *[]T, toLen int)
- func Slice[T any](items ...T) []T
- func SlicesEqual[T comparable](list1 []T, list2 []T) bool
- func TestExpect(t *testing.T, cond bool, msg string) bool
- func TestExpectf(t *testing.T, cond bool, format string, args ...any) bool
- func TimeStamp3Format(now time.Time, roundToMinutes int) string
- func TrimSpace(s *string)
- func TryAndLog[T any](value T, err error) T
- func UnsafeRawBytes[T any](v *T) []byte
- func UnsafeRawBytesOffset[T, S any](v *T, memberPtr *S) []byte
- func UnsafeSliceBytes[T any](v []T) []byte
- func UnsafeString(b []byte) string
- func UnsafeStringBytes(s string) []byte
- type Floaty
- type Inty
- type Set
- type TypedArena
- type TypedBucket
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddExitCleanup ¶ added in v0.1.2
func AddExitCleanup(fn func())
func AllocAppend ¶
func AllocAppend[T any](list *[]T) *T
AllocAppend appends a zero item to the list and returns a pointer to the added item
func Append ¶
Append adds an element to the list and returns its index. It takes a pointer to the list and a bunch of items to append. The idea is to replace the common but inconvenient pattern of `list = append(list, item)`
func EnsureMapNotNil ¶
func EnsureMapNotNil[K comparable, V any](m *map[K]V)
EnsureMapNotNil will make the map if it's nil
func EnsureSliceNotNil ¶
func EnsureSliceNotNil[T any](m *[]T)
EnsureSliceNotNil will make the slice of it's nil
func ExitWithCleanup ¶ added in v0.1.2
func ExitWithCleanup(code int)
func GrowSlice ¶
GrowSlice increase the size of the slice and fills the new slots with the zero value. Does nothing if slice's length is already equal or greater than the requested size
func IndexOf ¶
func IndexOf[T comparable](list []T, item T) int
func InitMap ¶
func InitMap[K comparable, V any](m *map[K]V)
InitMap is short hand for `m = make(map[K]V)` so you don't have to type out the full types. Just call `generic.InitMap(&m)`
func InitSlice ¶
func InitSlice[T any](m *[]T)
InitSlice is short hand for `list = make(list[T])` so you don't have to type out the full type. Just call `generic.InitSlice(&list)`
func InsertAt ¶
InsertAt is a generic function to insert an item (or multiple items) at a certain position in the list
func IsBetween ¶
func IsBetween[T numeric](start, item, end T) bool
is `item` in the range [start, end)
func LogWarningf ¶
func MapEntry ¶
func MapEntry[K comparable, V any](m map[K]V, key K, fn func(k K) V) V
Get a map entry with a function to create it if not existing
func MustNotNil ¶
func MustNotNil[T any](p *T) *T
func OneOf ¶
func OneOf[T comparable](item T, list []T) bool
OneOf checks whether an item is present in the list by iterating the list and returning true as soon as it finds the item
func ReadFromJSONFile ¶ added in v0.1.1
ReadFromJSONFile fills in an object with data from the json provied by the file specified
func SetupSigTermCleanup ¶ added in v0.1.2
func SetupSigTermCleanup()
func SharedPrefix ¶
SharedPrefix Extracts the shared prefix from two strings. Generated by ChatGPT
func SharedSuffix ¶
SharedSuffix extracts the shared suffix from two strings. Generated by ChatGPT
func ShrinkTo ¶
ShrinkTo is a safe version of `list = list[:toLen]` which would panic if the desired length is bigger than the length of the target list
func Slice ¶
func Slice[T any](items ...T) []T
Slice simplifies the syntax of the slice literal by removing the ugly curly braces. Instead of `[]int{1, 2, 3}` you can call `generic.Slice(1, 2, 3)`
func SlicesEqual ¶
func SlicesEqual[T comparable](list1 []T, list2 []T) bool
func TimeStamp3Format ¶
TimeStamp3Format takes a time and formats it as yyyy-mmdd-hhmm with the option to round to a multiple of minutes. For example, if you pass "2024-05-28 14:33" and round to 10 minutes, you get "2024-0528-1430"
func UnsafeRawBytes ¶
UnsafeRawBytes returns the raw bytes behind the object
func UnsafeRawBytesOffset ¶
UnsafeRawBytesOffset returns the raw bytes behind an object, skipping the first few fields
func UnsafeSliceBytes ¶
UnsafeSliceBytes takes a slice of any type and reinterprets it as a slice of bytes
func UnsafeString ¶
UnsafeString converts a byte slice to a string without copying
func UnsafeStringBytes ¶
UnsafeStringBytes converts a string to a byte slice without copying
Types ¶
type Set ¶
type Set[T comparable] struct { Map map[T]bool }
func NewSet ¶
func NewSet[T comparable]() *Set[T]
func NewSetFrom ¶
func NewSetFrom[T comparable](items []T) *Set[T]
type TypedArena ¶
type TypedArena[T any] struct { First *TypedBucket[T] Current *TypedBucket[T] }
func NewTypedArena ¶
func NewTypedArena[T any]() *TypedArena[T]
func (*TypedArena[T]) Allocate ¶
func (a *TypedArena[T]) Allocate() *T
func (*TypedArena[T]) Iterate ¶
func (a *TypedArena[T]) Iterate(visitFn func(index int, item *T) bool)
func (*TypedArena[T]) IterateBuckets ¶
func (a *TypedArena[T]) IterateBuckets(visitFn func(items []T))
type TypedBucket ¶
type TypedBucket[T any] struct { Items [4 * 1024]T Next int NextBucket *TypedBucket[T] }