utils

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2020 License: BSD-2-Clause Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs(x int) int

Abs returns the absolute value of x.

func After

func After(value string, a string) string

After returns the substring from value which comes after a.

func AfterSpace

func AfterSpace(value string) string

AfterSpace returns the substring from value which comes after the first whitespace.

func Before

func Before(value string, a string) string

Before returns the substring from value which comes before a.

func BeforeSpace

func BeforeSpace(value string) string

BeforeSpace returns the substring from value which comes before the first whitespace.

func ContainsInt

func ContainsInt(lst []int, x int) bool

ContainsInt returns whether the list contains the x-element, or not.

func CountLines

func CountLines(filename string) (int, error)

CountLines efficiently counts the lines of text inside a file. See: https://stackoverflow.com/questions/24562942/golang-how-do-i-determine-the-number-of-lines-in-a-file-efficiently

func DeserializeFromFile

func DeserializeFromFile(filename string, obj Deserializer) (err error)

DeserializeFromFile deserializes obj from file.

func ForEachField

func ForEachField(i interface{}, callback func(field interface{}, name string, tag reflect.StructTag))

ForEachField calls the callback for each field of the struct i.

func GetNeighborsIndices

func GetNeighborsIndices(size, index, windowSize int) []int

GetNeighborsIndices returns the indices of the (circular) neighbours at position index.

func IsStruct

func IsStruct(i interface{}) bool

IsStruct returns wether i is a struct.

func MakeIndices

func MakeIndices(size int) []int

MakeIndices returns a slice of the given size, where each element has the same value of its own index position.

func MakeIntMatrix

func MakeIntMatrix(rows, cols int) [][]int

MakeIntMatrix returns a new 2-dimensional slice of int.

func MinInt

func MinInt(a, b int) int

MinInt returns the minimum value between a and b.

func Name

func Name(i interface{}) string

Name returns the name of i.

func ReadFull

func ReadFull(r io.Reader, buf []byte) (int, error)

ReadFull reads from r into buf until it has read len(buf). It returns the number of bytes copied and an error if fewer bytes were read. If an EOF happens after reading fewer than len(buf) bytes, io.ErrUnexpectedEOF is returned.

func ReverseInPlace

func ReverseInPlace(s interface{})

ReverseInPlace reverses the elements of s (usually a slice).

func ReverseIntSlice

func ReverseIntSlice(lst []int) []int

ReverseIntSlice returns a reversed version of the given slice.

func ReverseString

func ReverseString(text string) string

ReverseString reverses the given string.

func SerializeToFile

func SerializeToFile(filename string, obj Serializer) (err error)

SerializeToFile serializes obj to file.

func SplitByRune

func SplitByRune(str string) []string

SplitByRune splits the input string by runes, and produces a new slice of string, where each item is one rune converted to string.

func SumInt

func SumInt(v []int) (s int)

SumInt returns the sum of all values from v.

func TypeName

func TypeName(instance interface{}) string

TypeName returns the type name of instance.

Types

type Deserializer

type Deserializer interface {
	Deserialize(r io.Reader) (int, error)
}

Deserializer is implemented by any value that has the Deserialize method.

type Pool

type Pool struct {

	// New optionally specifies a function to generate
	// a value when Get would otherwise return nil.
	New func() interface{}
	// contains filtered or unexported fields
}

Pool is a naive implementation of a pool, based on broadcast channel. You can use this pool in the same way you use sync.Pool.

func NewPool

func NewPool(max int) *Pool

NewPool returns a new pool ready to use. The pool can contain up to a max number of items. Set the property New to generate new items when needed during the Get.

func (*Pool) Get

func (p *Pool) Get() interface{}

Get selects an item from the Pool, removes it from the Pool, and returns it to the caller. If the poll is empty, Get returns the result of calling p.New.

func (*Pool) Put

func (p *Pool) Put(x interface{})

Put adds x to the pool.

type Serializer

type Serializer interface {
	Serialize(w io.Writer) (int, error)
}

Serializer is implemented by any value that has the Serialize method.

type Slice

type Slice struct {
	sort.Interface
	Indices []int
}

Slice is an extension of sort.Interface which keeps track of the original indices of the elements of a slice after sorting.

func NewFloat64Slice

func NewFloat64Slice(n ...float64) *Slice

NewFloat64Slice returns a new Slice for the given sequence of float64 values.

func NewIntSlice

func NewIntSlice(n ...int) *Slice

NewIntSlice returns a new Slice for the given sequence of int values.

func NewSlice

func NewSlice(n sort.Interface) *Slice

NewSlice returns a new Slice.

func NewStringSlice

func NewStringSlice(n ...string) *Slice

NewStringSlice returns a new Slice for the given sequence of string values.

func (Slice) Swap

func (s Slice) Swap(i, j int)

Swap swaps the elements with indexes i and j.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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