data

package
v1.2.35 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2026 License: AGPL-3.0 Imports: 18 Imported by: 4

Documentation

Overview

Package data provides custom data structures

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMarshalNil           = errors.New("object is nil")
	ErrMarshalInvalid       = errors.New("object is invalid")
	ErrMarshalType          = errors.New("invalid object type")
	ErrMarshalNoSize        = errors.New("missing/invalid size tag on field")
	ErrMarshalNoOpt         = errors.New("missing/invalid opt tag on field")
	ErrMarshalSizeMismatch  = errors.New("size mismatch during unmarshal")
	ErrMarshalEmptyIntf     = errors.New("can't handle empty interface")
	ErrMarshalUnknownType   = errors.New("unknown field type")
	ErrMarshalMthdMissing   = errors.New("missing method")
	ErrMarshalFieldRef      = errors.New("field reference invalid")
	ErrMarshalMthdNumArg    = errors.New("method has more than one argument")
	ErrMarshalMthdArgType   = errors.New("method argument not a string")
	ErrMarshalMthdResult    = errors.New("invalid method result")
	ErrMarshalParentMissing = errors.New("parent missing")
)

Errors

Functions

func Combinations added in v1.2.33

func Combinations[T any](in []T) (list [][]T)

Combinations generates (order-independent) lists from a given list of elements.

Example: in=[1,2,3] returns [1],[2],[3],[1,2],[1,3],[2,3],[1,2,3]

func CompareJSON added in v1.2.35

func CompareJSON(j1, j2 map[string]any) (list []string, err error)

CompareJSON compares to JSON objects and returns a list of differences between the two objects.

func Marshal

func Marshal(obj interface{}) ([]byte, error)

Marshal creates a byte array from an object.

func MarshalStream added in v1.2.15

func MarshalStream(wrt io.Writer, obj interface{}) error

MarshalStream writes an object to stream

func SExprAt added in v1.2.28

func SExprAt[T any](s *SExpr, n int) (v T, ok bool)

SExprAt returns the (typed) n.th element of the node

func Unmarshal

func Unmarshal(obj interface{}, data []byte) error

Unmarshal reads a byte array to fill an object pointed to by 'obj'.

func UnmarshalStream added in v1.2.15

func UnmarshalStream(rdr io.Reader, obj interface{}, pending int) error

UnmarshalStream reads an object from strean.

Types

type BloomFilter

type BloomFilter struct {
	BloomFilterBase

	Bits []byte `size:"(BitsSize)" json:"bits"` // bit storage
}

A BloomFilter is a space/time efficient set of unique entries. It can not enumerate its elements, but can check if an entry is contained in the set. The check always succeeds for a contained entry, but can create "false-positives" (entries not contained in the map give a positive result). By adjusting the number of bits in the BloomFilter and the number of indices generated for an entry, a BloomFilter can handle a given number of entries with a desired upper-bound for the false-positive rate.

func NewBloomFilter

func NewBloomFilter(numExpected int64, falsePositiveRate float64) *BloomFilter

NewBloomFilter creates a new BloomFilter based on the upper-bounds for the number of entries and the "false-positive" rate.

func NewBloomFilterDirect

func NewBloomFilterDirect(numBits int64, numIdx int) *BloomFilter

NewBloomFilterDirect creates a new BloomFilter based on the number of bits in the filter and the number of indices to be used.

func (*BloomFilter) Add

func (bf *BloomFilter) Add(entry []byte)

Add an entry to the BloomFilter.

func (*BloomFilter) BitsSize added in v1.2.20

func (bf *BloomFilter) BitsSize() uint

BitsSize returns the size of the byte array representing the filter bits.

func (*BloomFilter) Combine added in v1.1.1

func (bf *BloomFilter) Combine(bf2 *BloomFilter) *BloomFilter

Combine merges two BloomFilters (of same kind) into a new one.

func (*BloomFilter) Contains

func (bf *BloomFilter) Contains(entry []byte) bool

Contains returns true if the BloomFilter contains the given entry, and false otherwise. If an entry was added to the set, this function will always return 'true'. It can return 'true' for entries not in the set ("false-positives").

func (*BloomFilter) SameKind added in v1.1.1

func (bf *BloomFilter) SameKind(bf2 *BloomFilter) bool

SameKind checks if two BloomFilter have the same parameters.

func (*BloomFilter) Size added in v1.2.20

func (bf *BloomFilter) Size() uint

Size returns the size of the binary representation

type BloomFilterBase added in v1.2.23

type BloomFilterBase struct {
	NumBits    int64 `json:"numBits" order:"big"` // number of bits in filter ('m')
	NumIdx     uint8 `json:"numIdx"`              // number of indices ('k')
	NumIdxBits uint8 `json:"numIdxBits"`          // number of bits per index ('b')
	NumHash    uint8 `json:"numHash"`             // number of SHA256 hashes needed
}

BloomFilterBase for custom bloom filter implementations (e.g. simple, salted, counting, ...)

Parameters are usually computed from the number of expected entries 'n' and the error rate 'e' for false-positives:

k = ceil(-log2(e))
m = ceil(n*k / ln(2))
b = ceil(m/n)

func (*BloomFilterBase) Size added in v1.2.35

func (bf *BloomFilterBase) Size() uint

Size of the binary representation

type Combinator added in v1.2.33

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

Combinator yields lists of data assembled from source lists.

Example: data=[[1,7],[2,5,8],[4,9]] returns
[1,2,4],[7,2,4],[1,5,4],[7,5,4],[1,8,4],[7,8,4],
[1,2,9],[7,2,9],[1,5,9],[7,5,9],[1,8,9],[7,8,9]

func NewCombinator added in v1.2.33

func NewCombinator[T any](list [][]T) *Combinator[T]

NewCombinator from a list of index lists.

func (*Combinator[T]) Next added in v1.2.33

func (ci *Combinator[T]) Next() (res []T, done bool)

Next returns the next list combination

type CountingBloomFilter added in v1.2.23

type CountingBloomFilter struct {
	BloomFilterBase

	Counts []uint32 `size:"(NumBits)" json:"bits"` // counter storage
}

CountingBloomFilter is an extension of a generic bloomfilter that keeps a count instead of a single bit for masking. This allows the deletion of entries for the cost of 32x emory increase.

func NewCountingBloomFilter added in v1.2.23

func NewCountingBloomFilter(numExpected int64, falsePositiveRate float64) *CountingBloomFilter

NewCountingBloomFilter creates a new BloomFilter based on the upper-bounds for the number of entries and the "false-positive" rate.

func NewCountingBloomFilterDirect added in v1.2.23

func NewCountingBloomFilterDirect(numBits int64, numIdx int) *CountingBloomFilter

NewCountingBloomFilterDirect creates a new BloomFilter based on the number of bits in the filter and the number of indices to be used.

func (*CountingBloomFilter) Add added in v1.2.23

func (bf *CountingBloomFilter) Add(entry []byte)

Add an entry to the BloomFilter.

func (*CountingBloomFilter) Combine added in v1.2.23

Combine merges two BloomFilters (of same kind) into a new one.

func (*CountingBloomFilter) Contains added in v1.2.23

func (bf *CountingBloomFilter) Contains(entry []byte) bool

Contains returns true if the BloomFilter contains the given entry, and false otherwise. If an entry was added to the set, this function will always return 'true'. It can return 'true' for entries not in the set ("false-positives").

func (*CountingBloomFilter) Remove added in v1.2.23

func (bf *CountingBloomFilter) Remove(entry []byte) bool

Remove an entry from the bloomfilter

func (*CountingBloomFilter) SameKind added in v1.2.23

func (bf *CountingBloomFilter) SameKind(bf2 *CountingBloomFilter) bool

SameKind checks if two BloomFilter have the same parameters.

func (*CountingBloomFilter) Size added in v1.2.23

func (bf *CountingBloomFilter) Size() uint

Size returns the size of the binary representation

type Curve added in v1.2.33

type Curve interface {
	N() *math.Int
	G() Point
	Inf() Point

	Add(p1, p2 Point) Point
	Mult(k *math.Int, p Point) Point
}

type Generator added in v1.2.28

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

Generator for elements of given type. A generator object only handles the boilerplate on behalf of a generator function.

Example:

	   g := NewGenerator(gen)
	   for s := range g.Run() {
             :
		     if ??? {
		         g.Stop()
		         break
		     }
	   }

func NewGenerator added in v1.2.28

func NewGenerator[T any](gen GeneratorFunction[T]) *Generator[T]

NewGenerator runs the generator function in a go-routine. Calling the Run() method on an instance returns a channel of given type where values can be retrieved. To terminate a running generator, call the Stop() method. After a generator has finished (either by itself or because it was stopped), it cannot be restarted.

func (*Generator[T]) Run added in v1.2.28

func (g *Generator[T]) Run() <-chan T

Run returns a (read) channel from the generator.

func (*Generator[T]) Stop added in v1.2.28

func (g *Generator[T]) Stop()

Stop the generator (no more values expected).

type GeneratorChannel added in v1.2.28

type GeneratorChannel[T any] chan T

GeneratorChannel is used as a link between the generator boilerplate and a generator function.

func (GeneratorChannel[T]) Done added in v1.2.28

func (g GeneratorChannel[T]) Done()

Done with this generator - no more values can be generated.

func (GeneratorChannel[T]) Yield added in v1.2.28

func (g GeneratorChannel[T]) Yield(val T) (ok bool)

Yield a value (computed in a generator function) for a consumer. If the return value is false, the consumer indicates no more values are expected - so the generator function can terminate immediately.

type GeneratorFunction added in v1.2.28

type GeneratorFunction[T any] func(GeneratorChannel[T])

GeneratorFunction generates objects of given type and "yields" them to a channel. If the generator reaches end-of-output, it closes the channel. If yield returns false, the consumer has closed the channel to indicate it doesn't want any new objects.

Example:

	   func gen(out GeneratorChannel[string]) {
		   for i := 0; i < 100; i++ {
               val := fmt.Sprintf("%d", i+1)
		       if !out.Yield(val) {
		           return
	           }
	       }
	       out.Done()
	   }

type Kcheck added in v1.2.33

type Kcheck func(f []*math.Int, pos int64) int64

Kcheck is a callback to check if a sequence element is recurring. It is the task of the calback to compute the element from the list of factors and the initial values.

type Knacci added in v1.2.33

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

Knacci generates a k-step Fibonacci sequence in a cyclic group C_n with elements S_i. Each S_i is a list of factors S_{i,j} (0 <= j < k). The k-nacci result v_i is computed from the initial values a_j as: v_i = Sum_{j=1}^{k}(S_{i,j}*a_j)

func NewKnacci added in v1.2.33

func NewKnacci(k int, N *math.Int) (kn *Knacci)

NewKnacci instantiates a new k-nacci on the group modulo N

func ReadKnacci added in v1.2.33

func ReadKnacci(rdr io.Reader) (kn *Knacci, err error)

ReadKnacci creates a Knacci from data in a file

func (*Knacci) Factors added in v1.2.33

func (kn *Knacci) Factors(n int64) []*math.Int

Factors returns the factor list at given position. If the position is zero, the current factor list is returned.

func (*Knacci) Next added in v1.2.33

func (kn *Knacci) Next() (step int64, f []*math.Int)

Next returns the next position and factor list

func (*Knacci) Recurrence added in v1.2.33

func (kn *Knacci) Recurrence(depth int64, check Kcheck) (f []*math.Int, p1, p2 int64)

Recurrence searches for a recurrence in the Fibonacci sequence. If successful, it returns the positions of the matching element (p1,p2) and the factor list at p2.

func (*Knacci) Reset added in v1.2.33

func (kn *Knacci) Reset()

Reset instance to initial conditions.

func (*Knacci) Solve added in v1.2.33

func (kn *Knacci) Solve(f1, f2, init []*math.Int) *math.Int

Solve the equation for the first initial value

func (*Knacci) Steps added in v1.2.33

func (kn *Knacci) Steps() *math.Int

Steps returns the real number of steps

func (*Knacci) Write added in v1.2.33

func (kn *Knacci) Write(wrt io.Writer) (err error)

Write Knacci instance to writer

type KnacciECC added in v1.2.33

type KnacciECC struct {
	*Knacci
	// contains filtered or unexported fields
}

KnacciECC is a k-nacci sequence of points on an elliptic

func NewKnacciECC added in v1.2.33

func NewKnacciECC(c Curve, d Point, r []*math.Int) *KnacciECC

NewKnacciECC creates a k-nacci sequence over an elliptic

func (*KnacciECC) Recurrence added in v1.2.33

func (kn *KnacciECC) Recurrence(depth int64, descr string) (f []*math.Int, p1, p2 int64)

Recurrence detects a recurring point. The memory only spans a limit timeframe (1e6 steps), so recurrences could possibly be missed.

func (*KnacciECC) Solve added in v1.2.33

func (kn *KnacciECC) Solve(f1, f2 []*math.Int) *math.Int

Solve the equation for the scalar of the first initial point.

func (*KnacciECC) Value added in v1.2.33

func (kn *KnacciECC) Value(f []*math.Int) (n Point)

Value computes a point on E(p)

type KnacciInt added in v1.2.33

type KnacciInt struct {
	*Knacci
	// contains filtered or unexported fields
}

KnacciInt is a k-nacci integer sequence over a cyclic group C_n.

func NewKnacciInt added in v1.2.33

func NewKnacciInt(N *math.Int, init ...*math.Int) *KnacciInt

NewKnacciInt instantiates a new integer-based k-nacci.

func (*KnacciInt) Recurrence added in v1.2.33

func (kn *KnacciInt) Recurrence(depth int64, descr string) (f []*math.Int, p1, p2 int64)

Recurrence detects a recurring value. The memory only spans a limit timeframe (1e6 steps), so recurrences could possibly be missed.

func (*KnacciInt) Solve added in v1.2.33

func (kn *KnacciInt) Solve(f1, f2 []*math.Int) *math.Int

Solve the equation for the first initial value

func (*KnacciInt) Value added in v1.2.33

func (kn *KnacciInt) Value(f []*math.Int) (n *math.Int)

Value computes the current value of the sequence.

type Memory added in v1.2.32

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

func NewMemory added in v1.2.32

func NewMemory(num int, equal func(any, any) bool) *Memory

func (*Memory) Add added in v1.2.32

func (s *Memory) Add(e any)

func (*Memory) Contains added in v1.2.32

func (s *Memory) Contains(e any) int

type Permutation added in v1.2.29

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

Permutation of array elements

func NewPermutation added in v1.2.29

func NewPermutation[T any](in []T) *Permutation[T]

NewPermutation creates a shuffler for an array of elements

func (*Permutation[T]) Next added in v1.2.29

func (p *Permutation[T]) Next() (out []T, done bool)

Next returns the next permutation

func (*Permutation[T]) Num added in v1.2.33

func (p *Permutation[T]) Num() (n int64)

Num returns the number of permutations

type Point added in v1.2.33

type Point interface {
	Equals(Point) bool
}

type SExpr added in v1.2.28

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

SExpr is a node in a S-expression tree

func NewSExpr added in v1.2.28

func NewSExpr() *SExpr

NewSExpr creates an empty node

func ParseCSExpr added in v1.2.28

func ParseCSExpr(buf []byte) (root *SExpr, err error)

ParseCSExpr parses a canonical S-expression into a tree of nodes.

func ParseSExpr added in v1.2.28

func ParseSExpr(line string) (root *SExpr, err error)

ParseSExpr parses a text-only S-expression into a tree of nodes.

func (*SExpr) Drop added in v1.2.28

func (n *SExpr) Drop()

Drop node including children

func (*SExpr) Find added in v1.2.28

func (n *SExpr) Find(key string) *SExpr

Find node by tag (first element)

func (*SExpr) String added in v1.2.28

func (n *SExpr) String() (s string)

String returns a human-readable S-expression tree

type SaltedBloomFilter added in v1.2.20

type SaltedBloomFilter struct {
	Salt []byte `size:"4"` // salt value
	BloomFilter
}

SaltedBloomFilter is a bloom filter where each entr is "salted" with a uint32 salt value before processing. As each filter have different salts, the same set of entries added to the filter will result in a different bit pattern for the filter resulting in different false- positives for the same set. Useful if a filter is repeatedly generated for the same (or similar) set of entries.

func NewSaltedBloomFilter added in v1.2.20

func NewSaltedBloomFilter(salt uint32, numExpected int64, falsePositiveRate float64) *SaltedBloomFilter

NewSaltedBloomFilter creates a new salted BloomFilter based on the upper-bounds for the number of entries and the "false-positive" rate.

func NewSaltedBloomFilterDirect added in v1.2.20

func NewSaltedBloomFilterDirect(salt uint32, numBits int64, numIdx int) *SaltedBloomFilter

NewSaltedBloomFilterDirect creates a new salted BloomFilter based on the number of bits in the filter and the number of indices to be used.

func (*SaltedBloomFilter) Add added in v1.2.20

func (bf *SaltedBloomFilter) Add(entry []byte)

Add an entry to the BloomFilter.

func (*SaltedBloomFilter) Combine added in v1.2.20

Combine merges two salted BloomFilters (of same kind) into a new one.

func (*SaltedBloomFilter) Contains added in v1.2.20

func (bf *SaltedBloomFilter) Contains(entry []byte) bool

Contains returns true if the salted BloomFilter contains the given entry, and false otherwise. If an entry was added to the set, this function will always return 'true'. It can return 'true' for entries not in the set ("false-positives").

func (*SaltedBloomFilter) Size added in v1.2.20

func (bf *SaltedBloomFilter) Size() uint

Size returns the size of the binary representation

type SlidingBloomFilter added in v1.2.35

type SlidingBloomFilter struct {
	NumShards uint8          `json:"numShards"`
	NumIdx    uint8          `json:"numIdx"`
	NumDiv    int64          `json:"numDiv"`
	NumBits   int64          `json:"numBits"`
	Count     int64          `json:"Count"`
	Shards    []*BloomFilter `json:"shards"`
}

SlidingBloomFilter allows to add an unlimited number of entries to the BloomFilter, but only the last 'numExpected' entries are kept in the filter. The filter is composed of multiple shards that hold a portion of the added entries. If a shard is "full", a new shard is inserted and the oldes shard is dropped.

func NewSlidingBloomFilter added in v1.2.35

func NewSlidingBloomFilter(numShards int, numExpected int64, falsePositiveRate float64) *SlidingBloomFilter

NewSlidingBloomFilter creates a new BloomFilter based on the upper-bounds for the number of entries and the "false-positive" rate.

func (*SlidingBloomFilter) Add added in v1.2.35

func (bf *SlidingBloomFilter) Add(entry []byte)

Add an entry to the BloomFilter. If the number of entries in the primary shard is reached, a new shard is insert at the beginning (to become the new primary shard) and the oldest shard is removed.

func (*SlidingBloomFilter) Contains added in v1.2.35

func (bf *SlidingBloomFilter) Contains(entry []byte) bool

Contains returns true if the BloomFilter contains the given entry, and false otherwise. If an entry was added to the set, this function will always return 'true'. It can return 'true' for entries not in the set ("false-positives").

func (*SlidingBloomFilter) Size added in v1.2.35

func (bf *SlidingBloomFilter) Size() (s uint)

Size returns the size of the binary representation

type Stack

type Stack[T comparable] struct {
	// contains filtered or unexported fields
}

Stack for generic data types.

func NewStack

func NewStack[T comparable]() *Stack[T]

NewStack instantiates a new generic Stack object.

func (*Stack[T]) IsTop added in v1.2.35

func (s *Stack[T]) IsTop(v T) bool

IsTop compares the last element with given value.

func (*Stack[T]) Len

func (s *Stack[T]) Len() int

Len returns the number of elements on stack.

func (*Stack[T]) Peek

func (s *Stack[T]) Peek() (v T)

Peek at the last element pushed to stack without dropping it.

func (*Stack[T]) Pop

func (s *Stack[T]) Pop() (v T)

Pop last entry from stack and return it to caller.

func (*Stack[T]) Push

func (s *Stack[T]) Push(v T)

Push generic entry to stack.

type Vector

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

Vector data structure

func NewVector

func NewVector() *Vector

NewVector instantiates a new (empty) Vector object.

func (*Vector) Add

func (vec *Vector) Add(v interface{})

Add element to the end of the vector.

func (*Vector) At

func (vec *Vector) At(i int) (v interface{})

At return the indexed element from vector.

func (*Vector) Delete

func (vec *Vector) Delete(i int) (v interface{})

Delete indexed element from the vector.

func (*Vector) Drop

func (vec *Vector) Drop() (v interface{})

Drop the last element from the vector.

func (*Vector) Insert

func (vec *Vector) Insert(i int, v interface{})

Insert element at given position. Add 'nil' elements if index is beyond the end of the vector.

func (*Vector) Len

func (vec *Vector) Len() int

Len returns the number of elements in the vector.