utils

package
v0.1.7-0...-7628261 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2023 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const MIN_CACHE_SIZE = 10

Variables

This section is empty.

Functions

func Append

func Append(w Writeable, folder, fname string) error

Saves the Writeable object to the file specified. Appends to any existing file.

func BinarySearch

func BinarySearch[T any](sorted []T, tgt T, comparator func(T, T) int) (bool, int)

Performs a binary search on a sorted slice using the provided comparator. The comparator should be of the form:

func(left T, right T) int {
    if (left == right) { return 0 }
    if (left <  right) { return -1 }
    if (left >  right) { return 1 }  }

func DirExists

func DirExists(dir string) bool

func InsertToSortedList

func InsertToSortedList[T any](sorted []T, elem T, comparator func(T, T) int) []T

Inserts the provided element at the appropriate location in a sorted slice. Mutates the slice with the insert and returns it.

func Load

func Load(l Loadable, folder, fname string) error

This loads the data found in save_dir/folder/fname (or the most recently modified file in save_dir/folder if no fname is provided) and unmarshals it to the provided Loadable object.

func LoadBytes

func LoadBytes(folder, fname string) ([]byte, error)

Loads saved bytes from the silvermint save_dir/folder. If a filename is provided, this gets the data in save_dir/folder/fname. If an empty filename is provided, this gets the data from the most recently modified file found in save_dir/folder.

func Max

func Max[T constraints.Ordered](l, r T) T

Returns the maximum of two args. If they're equal returns the second one.

func MaybeCreateDir

func MaybeCreateDir(dir string, perm fs.FileMode) error

func Min

func Min[T constraints.Ordered](l, r T) T

Returns the minimum of two args. If they're equal returns the second one.

func Overwrite

func Overwrite(w Writeable, folder, fname string) error

Saves the Writeable object to the file specified. Overwrites any existing file. TODO(chuck): This should change the original file to .old, write to a new file, and then only delete the .old file once the write to the new file is complete. That way we don't corrupt the file if a crash happens during the write.

func Reducer

func Reducer[T any](
	combinable []T,
	combiner func(T, T) T,
) []T

Launches len(combinable)/2 go-routines, each of which combines two items together. Returns the log2(len(combinable)) results once all go-routines have concluded.

func ReducerWorker

func ReducerWorker[T any](prev T,
	cur T,
	reduced *T,
	combiner func(T, T) T,
	wg *sync.WaitGroup,
)

func Save

func Save(w Writeable, folder, fname string) error

Saves the Writeable object to the file specified. Does nothing if the file already exists.

func SliceDelete

func SliceDelete[T comparable](slice []T, i int) []T

Takes a slice and returns a new slice with the given entry removed. If you want to modify the slice you have, then use the `Delete()` function from `golang.org/x/exp/slices`. The slice returned is a new slice. Note that simple types of T will act like a completely new copy but for pointer types, the two slices will share entries.

func SliceInsert

func SliceInsert[T any](slice []T, i int, val T) []T

Takes a slice and returns a new slice with the given entry inserted at the provided index

func SliceMergeUnique

func SliceMergeUnique[T comparable](left []T, right []T) []T

Take two slices and merge them into a new slice that contains no duplicate entries. Note that duplicates will be removed even if the duplicate appears in the same list.

Example:

left := []int{1, 2, 3, 4, 4}
right := []int{3, 5, 6}
SliceMergeUnique(left, right) => []int{1, 2, 3, 4, 5, 6}

Types

type Cache

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

func NewCache

func NewCache[T any](size uint64) *Cache[T]

func (*Cache[T]) Get

func (c *Cache[T]) Get(el Keyable) (T, bool)

Get(Keyable) returns the object associated with that element and a boolean indicating true ONLY IF the value is currently in the buffer. If the object is not currently in the buffer it returns the zero value for that object and false.

func (*Cache[T]) Has

func (c *Cache[T]) Has(el Keyable) bool

Has(Keyable) tells us if, as of this check, the element is in the cache. There's no guarantee that it will remain in the cache however, and so if you intend to actually access it you should instead use Get(Keyable) and check the boolean returned by that function instead.

func (*Cache[T]) RangeRead

func (c *Cache[T]) RangeRead(vid uint16, f func(T) bool)

RangeRead(uint16, f(T) bool) calls f(T) over the elements stored in the sequence associated with the provided ValId, stopping when f(T) returns false or when all elements have been hit. Should not be used with any f(T) that changes the data in the sequence.

func (*Cache[T]) Seen

func (c *Cache[T]) Seen(el Keyable) bool

Seen(Keyable) tells if the cache has ever seen the entry corresponding to Keyable. It might not actually be in the cache currently.

func (*Cache[T]) Set

func (c *Cache[T]) Set(el Keyable, t T)

Set(Keyable, T) sets the value in sequence[Keyable.ValId()] at element Keyable.Height() to value T. Elements MUST be set in order, such that all elements [0...Keyable.Height()-1] have been added to the cache prior to attemption to add the element at Keyable.Height().

func (*Cache[T]) Size

func (c *Cache[T]) Size() uint64

Size() returns the sum of the size of all sequences. This assumes that all elements [0, ..., el.Latest] have been added to the sequence.

func (*Cache[T]) Tip

func (c *Cache[T]) Tip(vid uint16) T

Tip(uint16) returns the latest element in the sequence associated with the provided ValId.

type ConcurrentSortedSlice

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

A sorted list of Sortable objects that permits concurrency

func NewConcurrentSortedSlice

func NewConcurrentSortedSlice[T any]() *ConcurrentSortedSlice[T]

Creates a single new ConcurrentSortedSlice[T]

func NewConcurrentSortedSliceArray

func NewConcurrentSortedSliceArray[T any](n int) []*ConcurrentSortedSlice[T]

Creates a new slice of n ConcurrentSortedSlice[T]s

func (*ConcurrentSortedSlice[T]) Add

func (cs *ConcurrentSortedSlice[T]) Add(key Keyable, val T)

Adds the element val to the ConcurrentSortedSlice, sorted on the key

func (*ConcurrentSortedSlice[T]) Pop

func (cs *ConcurrentSortedSlice[T]) Pop() (T, bool)

Pops the first element of the ConcurrentSortedSlice, returning the associated value, or an empty value if there were none left. Returns a boolean of whether there were any left.

func (*ConcurrentSortedSlice[T]) Size

func (cs *ConcurrentSortedSlice[T]) Size() int

Returns the size of the ConcurrentSortedSlice

type Keyable

type Keyable interface {
	ValId() uint16
	Height() uint64
}

type Loadable

type Loadable interface {
	UnmarshalBinary([]byte) error
}

Types that are able to unmarshal their data from binary are Loadable, allowing them to be loaded from disk using the functions here.

type Sortable

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

Permits sorting the values val based on the Keyable value key

type Writeable

type Writeable interface {
	MarshalBinary() ([]byte, error)
}

Types that are able to marshal their data to binary are Writeable, allowing them to be written to disk using the functions here.

Directories

Path Synopsis
The list package implements a doubly-linked list of a type parameterized on any go value.
The list package implements a doubly-linked list of a type parameterized on any go value.

Jump to

Keyboard shortcuts

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