Run NodesReference

utils

Package utils

import "github.com/luxfi/node/utils"

Functions

DeleteIndex

func DeleteIndex[S ~[]E, E any](s S, i int) S

DeleteIndex moves the last element in the slice to index [i] and shrinks the size of the slice by 1.

This is an O(1) operation that allows the removal of an element from a slice when the order of the slice is not important.

If [i] is out of bounds, this function will panic.

Detach

func Detach(ctx context.Context) context.Context

Err

func Err(errors ...error) error

GetStacktrace

func GetStacktrace(all bool) string

IsSortedAndUnique

func IsSortedAndUnique[T Sortable[T]](s []T) bool

Returns true iff the elements in [s] are unique and sorted.

IsSortedAndUniqueByHash

func IsSortedAndUniqueByHash[T ~[]byte](s []T) bool

Returns true iff the elements in [s] are unique and sorted based by their hashes.

IsSortedAndUniqueOrdered

func IsSortedAndUniqueOrdered[T cmp.Ordered](s []T) bool

Returns true iff the elements in [s] are unique and sorted.

IsSortedBytes

func IsSortedBytes[T ~[]byte](s []T) bool

Returns true iff the elements in [s] are sorted.

RandomBytes

func RandomBytes(n int) []byte

RandomBytes returns a slice of n random bytes Intended for use in testing

Sort

func Sort[T Sortable[T]](s []T)

Sorts the elements of [s].

SortByHash

func SortByHash[T ~[]byte](s []T)

Sorts the elements of [s] based on their hashes.

Zero

func Zero[T any]() (_ T)

Returns a new instance of a T.

Types

BytesPool

BytesPool tracks buckets of available buffers to be allocated. Each bucket allocates buffers of the following length:

0 1 3 7 15 31 63 127 ... MaxInt

In order to allocate a buffer of length 19 (for example), we calculate the number of bits required to represent 19 (5). And therefore allocate a slice from bucket 5, which has length 31. This is the bucket which produces the smallest slices that are at least length 19.

When replacing a buffer of length 19, we calculate the number of bits required to represent 20 (5). And therefore place the slice into bucket 4, which has length 15. This is the bucket which produces the largest slices that a length 19 slice can be used for.

func NewBytesPool() *BytesPool
func (p *BytesPool) Get(length int) *[]byte
func (p *BytesPool) Put(bytes *[]byte)

Sortable