Documentation ¶
Index ¶
- Constants
- func Append(w Writeable, folder, fname string) error
- func BinarySearch[T any](sorted []T, tgt T, comparator func(T, T) int) (bool, int)
- func DirExists(dir string) bool
- func InsertToSortedList[T any](sorted []T, elem T, comparator func(T, T) int) []T
- func Load(l Loadable, folder, fname string) error
- func LoadBytes(folder, fname string) ([]byte, error)
- func Max[T constraints.Ordered](l, r T) T
- func MaybeCreateDir(dir string, perm fs.FileMode) error
- func Min[T constraints.Ordered](l, r T) T
- func Overwrite(w Writeable, folder, fname string) error
- func Reducer[T any](combinable []T, combiner func(T, T) T) []T
- func ReducerWorker[T any](prev T, cur T, reduced *T, combiner func(T, T) T, wg *sync.WaitGroup)
- func Save(w Writeable, folder, fname string) error
- func SliceDelete[T comparable](slice []T, i int) []T
- func SliceInsert[T any](slice []T, i int, val T) []T
- func SliceMergeUnique[T comparable](left []T, right []T) []T
- type Cache
- type ConcurrentSortedSlice
- type Keyable
- type Loadable
- type Sortable
- type Writeable
Constants ¶
const MIN_CACHE_SIZE = 10
Variables ¶
This section is empty.
Functions ¶
func BinarySearch ¶
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 InsertToSortedList ¶
Inserts the provided element at the appropriate location in a sorted slice. Mutates the slice with the insert and returns it.
func Load ¶
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 ¶
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 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 ¶
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 Save ¶
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 ¶
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 (*Cache[T]) Get ¶
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 ¶
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 ¶
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 ¶
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 ¶
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().
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 Loadable ¶
Types that are able to unmarshal their data from binary are Loadable, allowing them to be loaded from disk using the functions here.