base

package
v0.4.6 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2022 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const NotId = int32(-1)

NotId represents an ID doesn't exist.

Variables

This section is empty.

Functions

func CheckPanic

func CheckPanic()

CheckPanic catches panic.

func Escape

func Escape(text string) string

Escape text for csv.

func GetRandomName

func GetRandomName(retry int) string

GetRandomName generates a random name from the list of adjectives and surnames in this package formatted as "adjective_surname". For example 'focused_turing'. If retry is non-zero, a random integer between 0 and 10 will be added to the end of the name, e.g `focused_turing3`

func MarshalIndex

func MarshalIndex(w io.Writer, index Index) error

MarshalIndex marshal index into byte stream.

func NewMatrix32

func NewMatrix32(row, col int) [][]float32

NewMatrix32 creates a 2D matrix of 32-bit floats.

func NewMatrixInt

func NewMatrixInt(row, col int) [][]int

NewMatrixInt creates a 2D matrix of integers.

func RangeInt

func RangeInt(n int) []int

RangeInt generate a slice [0, ..., n-1].

func ReadLines

func ReadLines(sc *bufio.Scanner, sep string, handler func(int, []string) bool) error

ReadLines parse fields of each line for csv file.

func RepeatFloat32s

func RepeatFloat32s(n int, value float32) []float32

RepeatFloat32s repeats value n times.

func ValidateId

func ValidateId(text string) error

ValidateId validates user/item id. Id cannot be empty and contain [/,].

func ValidateLabel

func ValidateLabel(text string) error

ValidateLabel validates label. Label cannot be empty and contain [/,|].

Types

type Array

type Array[T any] struct {
	Data [][]T
}

func (*Array[T]) Append

func (a *Array[T]) Append(val T)

func (*Array[T]) Bytes

func (a *Array[T]) Bytes() int

func (*Array[T]) Get

func (a *Array[T]) Get(index int) T

func (*Array[T]) Len

func (a *Array[T]) Len() int

type DirectIndex

type DirectIndex struct {
	Limit int32
}

DirectIndex means that the name and its index is the same. For example, the index of "1" is 1, vice versa.

func NewDirectIndex

func NewDirectIndex() *DirectIndex

NewDirectIndex create a direct mapping index.

func (*DirectIndex) Add

func (idx *DirectIndex) Add(s string)

Add a name to current index.

func (*DirectIndex) Bytes

func (idx *DirectIndex) Bytes() int

func (*DirectIndex) GetNames

func (idx *DirectIndex) GetNames() []string

GetNames returns all names in current index.

func (*DirectIndex) Len

func (idx *DirectIndex) Len() int32

Len returns the number of names in current index.

func (*DirectIndex) Marshal

func (idx *DirectIndex) Marshal(w io.Writer) error

Marshal direct index into byte stream.

func (*DirectIndex) ToName

func (idx *DirectIndex) ToName(index int32) string

ToName converts a index to corresponding name.

func (*DirectIndex) ToNumber

func (idx *DirectIndex) ToNumber(name string) int32

ToNumber converts a name to corresponding index.

func (*DirectIndex) Unmarshal

func (idx *DirectIndex) Unmarshal(r io.Reader) error

Unmarshal direct index from byte stream.

type Index

type Index interface {
	Len() int32
	Add(name string)
	ToNumber(name string) int32
	ToName(index int32) string
	GetNames() []string
	Marshal(w io.Writer) error
	Unmarshal(r io.Reader) error
	Bytes() int
}

Index keeps the mapping between names (string) and indices (integer).

func UnmarshalIndex

func UnmarshalIndex(r io.Reader) (Index, error)

UnmarshalIndex unmarshal index from byte stream.

type MapIndex

type MapIndex struct {
	Numbers    map[string]int32 // sparse ID -> dense index
	Names      []string         // dense index -> sparse ID
	Characters int
}

MapIndex manages the map between sparse Names and dense indices. A sparse ID is a user ID or item ID. The dense index is the internal user index or item index optimized for faster parameter access and less memory usage.

func NewMapIndex

func NewMapIndex() *MapIndex

NewMapIndex creates a MapIndex.

func (*MapIndex) Add

func (idx *MapIndex) Add(name string)

Add adds a new ID to the indexer.

func (*MapIndex) Bytes

func (idx *MapIndex) Bytes() int

func (*MapIndex) GetNames

func (idx *MapIndex) GetNames() []string

GetNames returns all names in current index.

func (*MapIndex) Len

func (idx *MapIndex) Len() int32

Len returns the number of indexed Names.

func (*MapIndex) Marshal

func (idx *MapIndex) Marshal(w io.Writer) error

Marshal map index into byte stream.

func (*MapIndex) ToName

func (idx *MapIndex) ToName(index int32) string

ToName converts a dense index to a sparse ID.

func (*MapIndex) ToNumber

func (idx *MapIndex) ToNumber(name string) int32

ToNumber converts a sparse ID to a dense index.

func (*MapIndex) Unmarshal

func (idx *MapIndex) Unmarshal(r io.Reader) error

Unmarshal map index from byte stream.

type RandomGenerator

type RandomGenerator struct {
	*rand.Rand
}

RandomGenerator is the random generator for gorse.

func NewRandomGenerator

func NewRandomGenerator(seed int64) RandomGenerator

NewRandomGenerator creates a RandomGenerator.

func (RandomGenerator) NewNormalVector

func (rng RandomGenerator) NewNormalVector(size int, mean, stdDev float32) []float32

NewNormalVector makes a vec filled with normal random floats.

func (RandomGenerator) NormalMatrix

func (rng RandomGenerator) NormalMatrix(row, col int, mean, stdDev float32) [][]float32

NormalMatrix makes a matrix filled with normal random floats.

func (RandomGenerator) NormalMatrix64

func (rng RandomGenerator) NormalMatrix64(row, col int, mean, stdDev float64) [][]float64

NormalMatrix64 makes a matrix filled with normal random floats.

func (RandomGenerator) NormalVector64

func (rng RandomGenerator) NormalVector64(size int, mean, stdDev float64) []float64

NormalVector64 makes a vec filled with normal random floats.

func (RandomGenerator) Sample

func (rng RandomGenerator) Sample(low, high, n int, exclude ...*iset.Set) []int

Sample n values between low and high, but not in exclude.

func (RandomGenerator) SampleInt32

func (rng RandomGenerator) SampleInt32(low, high int32, n int, exclude ...*i32set.Set) []int32

SampleInt32 n 32bit values between low and high, but not in exclude.

func (RandomGenerator) UniformMatrix

func (rng RandomGenerator) UniformMatrix(row, col int, low, high float32) [][]float32

UniformMatrix makes a matrix filled with uniform random floats.

func (RandomGenerator) UniformVector

func (rng RandomGenerator) UniformVector(size int, low, high float32) []float32

UniformVector makes a vec filled with uniform random floats,

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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