utils

package
v0.30.0-rc0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2024 License: AGPL-3.0 Imports: 31 Imported by: 25

Documentation

Overview

Package utils contains all utility functions that currently have no better home than here. Consider moving them to go.viam.com/utils.

Index

Constants

View Source
const (
	// CredentialsTypeRobotSecret is for credentials used against the cloud managing this robot.
	CredentialsTypeRobotSecret = "robot-secret"

	// CredentialsTypeRobotLocationSecret is for credentials used against the cloud managing this robot's location.
	CredentialsTypeRobotLocationSecret = "robot-location-secret"
)
View Source
const (
	CLFMessageTypeParam            = CLFMessageType("PARAM")
	CLFMessageTypeComment          = CLFMessageType("COMMENT")
	CLFMessageTypeSync             = CLFMessageType("SYNC")
	CLFMessageTypeOdometry         = CLFMessageType("ODOM")
	CLFMessageTypeTruePos          = CLFMessageType("TRUEPOS")
	CLFMessageTypeRawLaser1        = CLFMessageType("RAWLASER1")
	CLFMessageTypeRawLaser2        = CLFMessageType("RAWLASER2")
	CLFMessageTypeRawLaser3        = CLFMessageType("RAWLASER3")
	CLFMessageTypeRawLaser4        = CLFMessageType("RAWLASER4")
	CLFMessageTypePositionLaser    = CLFMessageType("POSITIONLASER")
	CLFMessageTypeRobotLaser1      = CLFMessageType("ROBOTLASER1")
	CLFMessageTypeRobotLaser2      = CLFMessageType("ROBOTLASER2")
	CLFMessageTypeNMEAGGA          = CLFMessageType("NMEAGGA")
	CLFMessageTypeNMEARMC          = CLFMessageType("NMEARMC")
	CLFMessageTypeSonar            = CLFMessageType("SONAR")
	CLFMessageTypeBumper           = CLFMessageType("BUMPER")
	CLFMessageTypeScanMark         = CLFMessageType("SCANMARK")
	CLFMessageTypeIMU              = CLFMessageType("IMU")
	CLFMessageTypeVectorMove       = CLFMessageType("VECTORMOVE")
	CLFMessageTypeRobotVelocity    = CLFMessageType("ROBOTVELOCITY")
	CLFMessageTypeFollowTrajectory = CLFMessageType("FOLLOWTRAJECTORY")
	CLFMessageTypeBaseVelocity     = CLFMessageType("BASEVELOCITY")
	CLFMessageTypeOld              = CLFMessageType("OLD")
	CLFMessageTypeFrontLaser       = CLFMessageType("FLASER")
	CLFMessageTypeRearLaser        = CLFMessageType("RLASER")
	CLFMessageTypeLaser3           = CLFMessageType("LASER3")
	CLFMessageTypeLaser4           = CLFMessageType("LASER4")
	CLFMessageTypeRemissionFLaser  = CLFMessageType("REMISSIONFLASER")
	CLFMessageTypeRemissionRLaser  = CLFMessageType("REMISSIONRLASER")
	CLFMessageTypeRemissionLaser3  = CLFMessageType("REMISSIONLASER3")
	CLFMessageTypeRemissionLaser4  = CLFMessageType("REMISSIONLASER4")
)

known message types.

View Source
const (
	// DefaultResourceConfigurationTimeout is the default resource configuration
	// timeout.
	DefaultResourceConfigurationTimeout = time.Minute

	// ResourceConfigurationTimeoutEnvVar is the environment variable that can
	// be set to override DefaultResourceConfigurationTimeout as the duration
	// that resources are allowed to (re)configure.
	ResourceConfigurationTimeoutEnvVar = "VIAM_RESOURCE_CONFIGURATION_TIMEOUT"

	// DefaultModuleStartupTimeout is the default module startup timeout.
	DefaultModuleStartupTimeout = 5 * time.Minute

	// ModuleStartupTimeoutEnvVar is the environment variable that can
	// be set to override DefaultModuleStartupTimeout as the duration
	// that modules are allowed to startup.
	ModuleStartupTimeoutEnvVar = "VIAM_MODULE_STARTUP_TIMEOUT"

	// AndroidFilesDir is hardcoded because golang inits before our android code can override HOME var.
	AndroidFilesDir = "/data/user/0/com.viam.rdk.fgservice/cache"
)
View Source
const (
	// MimeTypeSuffixLazy is used to indicate a lazy loading of data.
	MimeTypeSuffixLazy = "lazy"

	// MimeTypeRawRGBA is for go's internal image.NRGBA. This uses the custom header as
	// explained in the comments for rimage.DecodeImage and rimage.EncodeImage.
	MimeTypeRawRGBA = "image/vnd.viam.rgba"

	// MimeTypeRawRGBALazy is a lazy MimeTypeRawRGBA.
	MimeTypeRawRGBALazy = MimeTypeRawRGBA + "+" + MimeTypeSuffixLazy

	// MimeTypeRawDepth is for depth images.
	MimeTypeRawDepth = "image/vnd.viam.dep"

	// MimeTypeJPEG is regular jpgs.
	MimeTypeJPEG = "image/jpeg"

	// MimeTypePNG is regular pngs.
	MimeTypePNG = "image/png"

	// MimeTypePCD is for .pcd pountcloud files.
	MimeTypePCD = "pointcloud/pcd"

	// MimeTypeQOI is for .qoi "Quite OK Image" for lossless, fast encoding/decoding.
	MimeTypeQOI = "image/qoi"

	// MimeTypeTabular used to indicate tabular data, this is used mainly for filtering data.
	MimeTypeTabular = "x-application/tabular"

	// MimeTypeDefault used if mimetype cannot be inferred.
	MimeTypeDefault = "application/octet-stream"

	// MimeTypeH264 used to indicate H264 frames.
	MimeTypeH264 = "video/h264"
)

Variables

FormatToMimeType maps Format to Mymetype.

MimeTypeToFormat maps Mymetype to Format.

View Source
var ParallelFactor = runtime.GOMAXPROCS(0)

ParallelFactor controls the max level of parallelization. This might be useful to set in tests where too much parallelism actually slows tests down in aggregate. MUST NOT be used with t.Parallel() or via init()!

Functions

func AbsInt

func AbsInt(n int) int

AbsInt returns the absolute value of the given value.

func AbsInt64

func AbsInt64(n int64) int64

AbsInt64 returns the absolute value of the given value.

func AngleDiffDeg

func AngleDiffDeg(a1, a2 float64) float64

AngleDiffDeg returns the closest difference from the two given angles. The arguments are commutative.

func AntiCWDeg

func AntiCWDeg(deg float64) float64

AntiCWDeg flips the given degrees as if you were to start at 0 and go counter-clockwise or vice versa.

func AssertType added in v0.2.36

func AssertType[T any](from interface{}) (T, error)

AssertType attempts to assert that the given interface argument is the given type parameter.

func BytesFromFloat32BE

func BytesFromFloat32BE(v float32) []byte

BytesFromFloat32BE converts a float32 to an array of bytes ordered in big-endian.

func BytesFromFloat32LE

func BytesFromFloat32LE(v float32) []byte

BytesFromFloat32LE converts a float32 to an array of bytes ordered in little-endian.

func BytesFromFloat64BE

func BytesFromFloat64BE(v float64) []byte

BytesFromFloat64BE converts a float64 to an array of bytes ordered in big-endian.

func BytesFromFloat64LE

func BytesFromFloat64LE(v float64) []byte

BytesFromFloat64LE converts a float64 to an array of bytes ordered in little-endian.

func BytesFromUint32BE

func BytesFromUint32BE(v uint32) []byte

BytesFromUint32BE converts a uint32 to an array of bytes ordered in big-endian.

func BytesFromUint32LE

func BytesFromUint32LE(v uint32) []byte

BytesFromUint32LE converts a uint32 to an array of bytes ordered in little-endian.

func CheckLazyMIMEType added in v0.0.8

func CheckLazyMIMEType(mimeType string) (string, bool)

CheckLazyMIMEType checks the lazy suffix of a MIME.

func Clamp

func Clamp(value, min, max float64) float64

Clamp returns min if value is lesser than min, max if value is greater them max or value if the input value is between min and max.

func ComputeDistance

func ComputeDistance(p1, p2 []float64, distType DistanceType) (float64, error)

ComputeDistance computes the distance between two vectors stored in a slice of floats.

func CubeRoot

func CubeRoot(x float64) float64

CubeRoot returns the cube root of the given value.

func CycleIntSliceByN

func CycleIntSliceByN(s []int, n int) []int

CycleIntSliceByN cycles the list to the right by n steps.

func DegToRad

func DegToRad(degrees float64) float64

DegToRad converts degrees to radians.

func EuclideanDistance

func EuclideanDistance(p1, p2 []float64) (float64, error)

EuclideanDistance computes the euclidean distance between 2 vectors.

func FilterMap added in v0.18.0

func FilterMap[K comparable, V any](orig map[K]V, predicate func(K, V) bool) map[K]V

FilterMap is a helper that returns a new map based on k,v pairs that pass predicate.

func FilterSlice added in v0.28.0

func FilterSlice[T any](orig []T, predicate func(T) bool) []T

FilterSlice returns a new slice with elements of `orig` which match `predicate`.

func FindInSlice added in v0.26.0

func FindInSlice[T any](items []T, predicate func(T) bool) *T

FindInSlice returns the first item in items which satisfies predicate, or nil.

func Float32FromBytesBE

func Float32FromBytesBE(bytes []byte) float32

Float32FromBytesBE converts an array of byte ordered in big-endian to a float32.

func Float32FromBytesLE

func Float32FromBytesLE(bytes []byte) float32

Float32FromBytesLE converts an array of byte ordered in little-endian to a float32.

func Float64AlmostEqual

func Float64AlmostEqual(a, b, epsilon float64) bool

Float64AlmostEqual compares two float64s and returns if the difference between them is less than epsilon.

func Float64FromBytesBE

func Float64FromBytesBE(bytes []byte) float64

Float64FromBytesBE converts an array of byte ordered in big-endian to a float64.

func Float64FromBytesLE

func Float64FromBytesLE(bytes []byte) float64

Float64FromBytesLE converts an array of byte ordered in little-endian to a float64.

func FlushChan added in v0.9.0

func FlushChan[T any](c chan T)

FlushChan is a function that takes a generic chanel and completely empties it.

func GetArgMinDistancesPerRow

func GetArgMinDistancesPerRow(distances *mat.Dense) []int

GetArgMinDistancesPerRow returns in a slice of int the index of the point with minimum distance for each row.

func GetArgMinDistancesPerRowInt added in v0.0.9

func GetArgMinDistancesPerRowInt(distances [][]int) []int

GetArgMinDistancesPerRowInt returns in a slice of int the index of the point with minimum distance for each row.

func GetDeviceInfo added in v0.2.47

func GetDeviceInfo(modelName string) (utils.StringSet, error)

GetDeviceInfo returns the device information in stringset.

func GetInParallel added in v0.8.0

func GetInParallel(ctx context.Context, fs []FloatFunc) (time.Duration, []float64, error)

GetInParallel runs all functions in parallel, return is elapsed time, a list of floats, and an error.

func GetModuleStartupTimeout added in v0.16.0

func GetModuleStartupTimeout(logger logging.Logger) time.Duration

GetModuleStartupTimeout calculates the module startup timeout (env variable value if set, DefaultModuleStartupTimeout otherwise).

func GetResourceConfigurationTimeout added in v0.5.1

func GetResourceConfigurationTimeout(logger logging.Logger) time.Duration

GetResourceConfigurationTimeout calculates the resource configuration timeout (env variable value if set, DefaultResourceConfigurationTimeout otherwise).

func GroupWorkParallel

func GroupWorkParallel(ctx context.Context, totalSize int, before BeforeParallelGroupWorkFunc, groupWork GroupWorkFunc) error

GroupWorkParallel parallelizes the given size of work over multiple workers.

func HammingDistance

func HammingDistance(p1, p2 []float64) (float64, error)

HammingDistance computes the hamming distance between two vectors that only contain zeros and ones.

func Int16FromBytesBE added in v0.2.5

func Int16FromBytesBE(bytes []byte) int16

Int16FromBytesBE converts an array of bytes ordered in big-endian to a (signed) int16.

func Int16FromBytesLE added in v0.2.5

func Int16FromBytesLE(bytes []byte) int16

Int16FromBytesLE converts an array of bytes ordered in little-endian to a (signed) int16.

func IsTrustedEnvironment added in v0.2.36

func IsTrustedEnvironment(ctx context.Context) bool

IsTrustedEnvironment is used to check the trusted state of the runtime. Note: by default, if no context is set up, trust is assumed; be careful.

func MMToMeters added in v0.15.0

func MMToMeters(mm float64) float64

MMToMeters converts a value in mm to a value in meters.

func MapOver added in v0.28.0

func MapOver[T, U any](items []T, fn func(T) (U, error)) ([]U, error)

MapOver applies fn() to a slice of items and returns a slice of the return values. TODO(golang/go#61898): use stdlib iter if it lands.

func MaxInt

func MaxInt(a, b int) int

MaxInt returns the maximum of two values.

func MaxUint8

func MaxUint8(a, b uint8) uint8

MaxUint8 returns the maximum of two values.

func Median

func Median(values ...float64) float64

Median returns the median value of the given values. If there are no values, NaN is returned.

func MetersToMM added in v0.15.0

func MetersToMM(m float64) float64

MetersToMM converts a value in meters to a value in mm.

func MinInt

func MinInt(a, b int) int

MinInt returns the minimum of two values.

func MinUint8

func MinUint8(a, b uint8) uint8

MinUint8 returns the minimum of two values.

func ModAngDeg

func ModAngDeg(ang float64) float64

ModAngDeg returns the given angle modulus 360 and resolves any negativity.

func NewBuildTimeoutError added in v0.16.0

func NewBuildTimeoutError(name string) error

NewBuildTimeoutError is used when a resource times out during construction or reconfiguration.

func NewFilePathDebugLogger added in v0.10.0

func NewFilePathDebugLogger(filepath, name string) (logging.Logger, error)

NewFilePathDebugLogger is intended as a debug only tool & should not be used in prod logs using debug configuration to log to both stderr, stdout & a filepath.

func NewModuleStartUpTimeoutError added in v0.16.0

func NewModuleStartUpTimeoutError(name string) error

NewModuleStartUpTimeoutError is used when a module times out during startup.

func NewRemoteResourceClashError

func NewRemoteResourceClashError(name string) error

NewRemoteResourceClashError is used when you are more than one resource with the same name exist.

func NewUnexpectedTypeError

func NewUnexpectedTypeError[ExpectedT any](actual interface{}) error

NewUnexpectedTypeError is used when there is a type mismatch.

func NewWeakDependenciesUpdateTimeoutError added in v0.21.0

func NewWeakDependenciesUpdateTimeoutError(name string) error

NewWeakDependenciesUpdateTimeoutError is used when a resource times out during weak dependencies update.

func PairwiseDistance

func PairwiseDistance(pts1, pts2 [][]float64, distType DistanceType) (*mat.Dense, error)

PairwiseDistance computes the pairwise distances between 2 sets of points.

func ParallelForEachPixel

func ParallelForEachPixel(size image.Point, f func(x, y int))

ParallelForEachPixel loops through the image and calls f functions for each [x, y] position. The image is divided into N * N blocks, where N is the number of available processor threads. For each block a parallel Goroutine is started.

func PlatformHomeDir added in v0.27.0

func PlatformHomeDir() string

PlatformHomeDir wraps Getenv("HOME"), except on android, where it returns the app cache directory.

func PlatformMkdirTemp added in v0.27.0

func PlatformMkdirTemp(dir, pattern string) (string, error)

PlatformMkdirTemp wraps MkdirTemp. On android, when dir is empty, it uses a path that is writable + executable.

func RadToDeg

func RadToDeg(radians float64) float64

RadToDeg converts radians to degrees.

func RemoveFileNoError added in v0.2.47

func RemoveFileNoError(path string)

RemoveFileNoError will remove the file at the given path if it exists. Any errors will be suppressed.

func ResolveFile

func ResolveFile(fn string) string

ResolveFile returns the path of the given file relative to the root of the codebase. For example, if this file currently lives in utils/file.go and ./foo/bar/baz is given, then the result is foo/bar/baz. This is helpful when you don't want to relatively refer to files when you're not sure where the caller actually lives in relation to the target file.

func RunInParallel

func RunInParallel(ctx context.Context, fs []SimpleFunc) (time.Duration, error)

RunInParallel runs all functions in parallel, return is elapsed time and an error.

func SafeJoinDir added in v0.29.0

func SafeJoinDir(parent, subdir string) (string, error)

SafeJoinDir performs a filepath.Join of 'parent' and 'subdir' but returns an error if the resulting path points outside of 'parent'. See also https://github.com/cyphar/filepath-securejoin.

func SampleNIntegersNormal

func SampleNIntegersNormal(n int, vMin, vMax float64) []int

SampleNIntegersNormal samples n integers from normal distribution centered around (vMax+vMin) / 2 and in range [vMin, vMax].

func SampleNIntegersUniform

func SampleNIntegersUniform(n int, vMin, vMax float64) []int

SampleNIntegersUniform samples n integers uniformly in [vMin, vMax].

func SampleNRegularlySpaced

func SampleNRegularlySpaced(n int, vMin, vMax float64) []int

SampleNRegularlySpaced returns the same set of evenly divided numbers every time, and is mostly used for testing purposes.

func SampleRandomIntRange

func SampleRandomIntRange(min, max int, r *rand.Rand) int

SampleRandomIntRange samples a random integer within a range given by [min, max] using the given rand.Rand.

func ScaleByPct

func ScaleByPct(n int, pct float64) int

ScaleByPct scales a max number by a floating point percentage between two bounds [0, n].

func SelectNIndicesWithoutReplacement

func SelectNIndicesWithoutReplacement(nSamples, nMax int) ([]int, error)

SelectNIndicesWithoutReplacement select N random indices from [0,nMax] without replacement (no duplicate indices).

func Single

func Single(dim int, x []float64) [][]float64

Single generates an n-dimensional Grid using a single set of values. dim specifies the number of dimensions, the entries in x specify the gridded values.

func SlowStartupLogger added in v0.26.0

func SlowStartupLogger(ctx context.Context, msg, fieldName, fieldVal string, logger logging.Logger) func()

SlowStartupLogger starts a goroutine that logs every few seconds as long as the context has not timed out or was not cancelled.

func Square

func Square(n float64) float64

Square returns the square of the given value. Math.pow( x, 2 ) is slow, this is faster.

func SquareInt

func SquareInt(n int) int

SquareInt returns the square of the given value. Math.pow( x, 2 ) is slow, this is faster.

func SubFor

func SubFor(sub []int, idx int, dims []int) []int

SubFor constructs the multi-dimensional subscript for the input linear index. dims specify the maximum size in each dimension. If sub is non-nil the result is stored in-place into sub. If it is nil a new slice of the appropriate length is allocated.

func Testing added in v0.19.0

func Testing() bool

Testing returns true when you are running in test suite.

func Transpose added in v0.0.9

func Transpose(slice [][]int) [][]int

Transpose transposes the slice of slice of ints.

func TypeStr added in v0.2.36

func TypeStr[T any]() string

TypeStr returns the a human readable type string of the given value.

func Uint32FromBytesBE

func Uint32FromBytesBE(bytes []byte) uint32

Uint32FromBytesBE converts an array of bytes ordered in big-endian to a uint32.

func Uint32FromBytesLE

func Uint32FromBytesLE(bytes []byte) uint32

Uint32FromBytesLE converts an array of bytes ordered in little-endian to a uint32.

func ValidateBaudRate added in v0.2.49

func ValidateBaudRate(validBaudRates []uint, baudRate int) bool

ValidateBaudRate validates that the baudrate is in the list of valid values.

func ValidateModuleName added in v0.23.0

func ValidateModuleName(name string) error

ValidateModuleName validates that the module follows our naming requirements. the module name is used to create the socket path, so if you modify this, ensure that this only accepts valid socket paths.

func ValidatePackageName added in v0.23.0

func ValidatePackageName(name string) error

ValidatePackageName validates that the package follows our naming requirements.

func ValidateRemoteName added in v0.23.0

func ValidateRemoteName(name string) error

ValidateRemoteName validates that the remote follows our naming requirements.

func ValidateResourceName added in v0.23.0

func ValidateResourceName(name string) error

ValidateResourceName validates that the resource follows our naming requirements.

func Walk

func Walk(middleX, middleY, maxRadius int, f WalkCallback) error

Walk starts at the given middle point and walks around increasingly bigger squares based on the given radius growing outwards.

func WithLazyMIMEType added in v0.0.8

func WithLazyMIMEType(mimeType string) string

WithLazyMIMEType attaches the lazy suffix to a MIME.

func WithTrustedEnvironment added in v0.2.36

func WithTrustedEnvironment(ctx context.Context, trusted bool) (context.Context, error)

WithTrustedEnvironment is used to inform environment trust across boundaries.

Types

type AttributeMap added in v0.2.36

type AttributeMap map[string]interface{}

An AttributeMap is a convenience wrapper for pulling out typed information from a map.

func (AttributeMap) Bool added in v0.2.36

func (am AttributeMap) Bool(name string, def bool) bool

Bool attempts to return a boolean present in the map with the given name; returns the given default otherwise.

func (AttributeMap) BoolSlice added in v0.2.36

func (am AttributeMap) BoolSlice(name string, def bool) []bool

BoolSlice attempts to return a slice of bools present in the map with the given name; returns an empty slice otherwise.

func (AttributeMap) Float64 added in v0.2.36

func (am AttributeMap) Float64(name string, def float64) float64

Float64 attempts to return a float64 present in the map with the given name; returns the given default otherwise.

func (AttributeMap) Float64Slice added in v0.2.36

func (am AttributeMap) Float64Slice(name string) []float64

Float64Slice attempts to return a slice of ints present in the map with the given name; returns an empty slice otherwise.

func (AttributeMap) Has added in v0.2.36

func (am AttributeMap) Has(name string) bool

Has returns whether or not the given name is in the map.

func (AttributeMap) Int added in v0.2.36

func (am AttributeMap) Int(name string, def int) int

Int attempts to return an integer present in the map with the given name; returns the given default otherwise.

func (AttributeMap) IntSlice added in v0.2.36

func (am AttributeMap) IntSlice(name string) []int

IntSlice attempts to return a slice of ints present in the map with the given name; returns an empty slice otherwise.

func (AttributeMap) String added in v0.2.36

func (am AttributeMap) String(name string) string

String attempts to return a string present in the map with the given name; returns an empty string otherwise.

func (AttributeMap) StringSlice added in v0.2.36

func (am AttributeMap) StringSlice(name string) []string

StringSlice attempts to return a slice of strings present in the map with the given name; returns an empty slice otherwise.

func (AttributeMap) Walk added in v0.2.36

func (am AttributeMap) Walk(visitor Visitor) (interface{}, error)

Walk implements the Walker interface.

type BeforeParallelGroupWorkFunc

type BeforeParallelGroupWorkFunc func(groupSize int)

BeforeParallelGroupWorkFunc executes before any work starts with the calculated group size.

type CLFBaseMessage

type CLFBaseMessage struct {
	MessageType     CLFMessageType
	IPCTimestamp    float64
	IPCHostname     string
	LoggerTimestamp float64
}

CLFBaseMessage is used by all messages and contains basic information about the message.

func (CLFBaseMessage) Base

func (b CLFBaseMessage) Base() CLFBaseMessage

Base returns the base part of the message.

func (CLFBaseMessage) Type

func (b CLFBaseMessage) Type() CLFMessageType

Type returns the type of message this is.

type CLFMessage

type CLFMessage interface {
	Base() CLFBaseMessage
	Type() CLFMessageType
}

CLFMessage is a specific type of CLF message that always has a base message.

type CLFMessageType

type CLFMessageType string

CLFMessageType describes a specific type of message.

type CLFOdometryMessage

type CLFOdometryMessage struct {
	CLFMessage
	X                     float64
	Y                     float64
	Theta                 float64
	TranslationalVelocity float64
	RotationalVelocity    float64
	Acceleration          float64
}

CLFOdometryMessage represents odometry data.

type CLFOldLaserMessage

type CLFOldLaserMessage struct {
	CLFMessage
	RangeReadings []float64
	X             float64
	Y             float64
	Theta         float64
	OdomX         float64
	OdomY         float64
	OdomTheta     float64
}

CLFOldLaserMessage represents legacy lidar scan data.

type CLFParamMessage

type CLFParamMessage struct {
	CLFMessage
	Name, Value string
}

CLFParamMessage conveys parameters being set for the whole CLF.

type CLFReader

type CLFReader struct {
	// contains filtered or unexported fields
}

A CLFReader can read in CARMEN Logfiles.

func NewCLFReader

func NewCLFReader(reader io.Reader) *CLFReader

NewCLFReader returns a CLF reader based on the given reader.

func (*CLFReader) Process

func (r *CLFReader) Process(f func(message CLFMessage) error) error

Process reads over all messages and calls the given function for each message. If the function returns an error, execution stops with that error returned to the caller.

type DistanceType

type DistanceType int

DistanceType defines the type of distance used in a function.

const (
	// Euclidean is DistanceType 0.
	Euclidean DistanceType = iota
	// Hamming is DistanceType 1.
	Hamming
)

type FloatFunc added in v0.8.0

type FloatFunc func(ctx context.Context) (float64, error)

FloatFunc is for GetInParallel.

type GroupWorkDoneFunc

type GroupWorkDoneFunc func()

GroupWorkDoneFunc runs when a single group's work is done; helpful for merge stages.

type GroupWorkFunc

type GroupWorkFunc func(groupNum, groupSize, from, to int) (MemberWorkFunc, GroupWorkDoneFunc)

GroupWorkFunc runs to determine what work members should do, if any.

type Guard added in v0.25.0

type Guard struct {
	OnFail func()
	// contains filtered or unexported fields
}

Guard is a structure for managing special cleanup for when a function that returns an allocated resource (e.g: open file) fails. Programmers are often presented with two options when dealing with early-return error cases:

  • Don't use defers. On success, the resource (e.g: file object) is returned, but on failure the programmer must ensure the resource (e.g: file) is closed.
  • Use defers to close the resource in anticipation of failure. But wrap the defer with an `if !success` check. And when the function returns for the success case, it must flip a `bool success` from false - > true.

A Guard encapsulates the second technique. Correct usage of a Guard uses the following pattern:

guard := NewGuard(func() { f.Close() })
defer guard.OnFail()
if (error) { return error }
guard.Success()
return nil

func NewGuard added in v0.25.0

func NewGuard(onFailCleanup func()) *Guard

NewGuard returns a NewGuard.

func (*Guard) Success added in v0.25.0

func (guard *Guard) Success()

Success declares the function succeeded and the "failure" cleanup code does not need to be executed.

type MemberWorkFunc

type MemberWorkFunc func(memberNum, workNum int)

MemberWorkFunc runs for each work item (member) of a group.

type Rand added in v0.19.0

type Rand interface {
	Float64() float64
}

Rand is a wrapper for either a rand.Rand or a pass-through to the shared rand.x functions.

func SafeTestingRand added in v0.19.0

func SafeTestingRand() Rand

SafeTestingRand returns a wrapper around the shared math/rand source in prod, and a deterministic rand.Rand seeded with 0 in test.

type RollingAverage

type RollingAverage struct {
	// contains filtered or unexported fields
}

RollingAverage computes an average in a moving window of a certain size. It is goroutine-safe but should be used for statistical purposes only due to the use of atomics and not mutexes.

func NewRollingAverage

func NewRollingAverage(windowSize int) *RollingAverage

NewRollingAverage returns a rolling average computed on the given window size.

func (*RollingAverage) Add

func (ra *RollingAverage) Add(x int)

Add adds the given value to the samples.

func (*RollingAverage) Average

func (ra *RollingAverage) Average() int

Average recomputes and returns the current rolling average.

func (*RollingAverage) NumSamples

func (ra *RollingAverage) NumSamples() int

NumSamples returns the number of samples currently collected.

type SimpleFunc

type SimpleFunc func(ctx context.Context) error

SimpleFunc is for RunInParallel.

type StoppableWorkers added in v0.26.0

type StoppableWorkers interface {
	AddWorkers(...func(context.Context))
	Stop()
	Context() context.Context
}

StoppableWorkers is a collection of goroutines that can be stopped at a later time.

func NewStoppableWorkers added in v0.26.0

func NewStoppableWorkers(funcs ...func(context.Context)) StoppableWorkers

NewStoppableWorkers runs the functions in separate goroutines. They can be stopped later.

type TypedName

type TypedName struct {
	Name string
	Type string
}

A TypedName stores both the name and type of the variable.

func JSONTags

func JSONTags(s interface{}) []TypedName

JSONTags returns a slice of strings of the variable names in the JSON tags of a struct.

type Vec2Fs

type Vec2Fs [][]float64

Vec2Fs is a series of two dimensional vectors that are float based.

func (Vec2Fs) Len

func (vs Vec2Fs) Len() int

Len returns the number of vectors.

func (Vec2Fs) Less

func (vs Vec2Fs) Less(i, j int) bool

Less returns which vector compares less by first checking if the X component is less, then if the X component is more, and finally if the Y component is less.

func (Vec2Fs) Swap

func (vs Vec2Fs) Swap(i, j int)

Swap swaps two vectors by positionally.

type Vec2Matrix

type Vec2Matrix mat.Dense

Vec2Matrix is a wrapper around a mat.Dense intended for a series of 2D vectors.

func (*Vec2Matrix) DistanceMSETo

func (v2m *Vec2Matrix) DistanceMSETo(to *Vec2Matrix) float64

DistanceMSETo returns the mean squared error between this matrix and the given matrix. This is helpful for calculating how "far off" two matrices are.

func (*Vec2Matrix) RotateMatrixAbout

func (v2m *Vec2Matrix) RotateMatrixAbout(x, y, theta float64) *Vec2Matrix

RotateMatrixAbout rotates every vector by the given theta about a given point.

type Visitor added in v0.2.36

type Visitor interface {
	// Visit visits a node and returns a new node, with or without modifications.
	Visit(interface{}) (interface{}, error)
}

Visitor defines an interface for visiting and potentially modifying portions of the config.

type WalkCallback

type WalkCallback func(x, y int) error

WalkCallback is to be called for each point visited by Walk.

type Walker added in v0.2.36

type Walker interface {
	// Walk walks a structure and returns a new structure, with or without modifications.
	Walk(Visitor) (interface{}, error)
}

Walker is a portion of the config that can be walked.

Directories

Path Synopsis
Package contextutils provides utilities for dealing with contexts such as adding and retrieving metadata to/from a context, and handling context timeouts.
Package contextutils provides utilities for dealing with contexts such as adding and retrieving metadata to/from a context, and handling context timeouts.
Package diskusage is used to get platform specific file system usage information.
Package diskusage is used to get platform specific file system usage information.
package main: this is called by the utils test suite to confirm the Testing() test is false in prod.
package main: this is called by the utils test suite to confirm the Testing() test is false in prod.

Jump to

Keyboard shortcuts

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