valuecounts

package
v0.0.0-...-892e02c Latest Latest
Warning

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

Go to latest
Published: Feb 29, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pair

type Pair struct {
	Value int `json:"value"`
	Count int `json:"count"`
}

Pair stores a single value and associated count pair

type ValueCounts

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

ValueCounts stores unordered counts of integer values as a map from value (int) to count (int). It can be serialized to JSON as an array of (value, count) pairs.

func Count

func Count(data []int) ValueCounts

Count produces a new ValueCounts by counting repetitions of values in the input data

func FromMap

func FromMap(data map[int]int) ValueCounts

FromMap creates a new ValueCounts object and initialises its counts from the given map

func FromPairs

func FromPairs(pairs []Pair) (ValueCounts, error)

FromPairs converts a list of (value, count) pairs back into ValueCounts. If the same value occurs multiple times in the list, an error is raised. If pairs is nil or empty but non-nil, an empty ValueCounts is returned.

func New

func New() ValueCounts

New creates a new empty ValueCounts object

func (ValueCounts) Len

func (vc ValueCounts) Len() int

Len returns the number of values stored by this ValueCounts. It is equivalent to the length of the slice returned by ToPairs()

func (ValueCounts) MarshalJSON

func (vc ValueCounts) MarshalJSON() ([]byte, error)

MarshalJSON serialises this ValueCounts into a JSON array of {value, count} pairs.

func (ValueCounts) String

func (vc ValueCounts) String() string

String() returns a string representation of this ValueCounts with values sorted in ascending order

func (ValueCounts) ToPairs

func (vc ValueCounts) ToPairs() []Pair

ToPairs converts this ValueCounts into a list of (value, count) pairs. The values are sorted in increasing order so that the output is deterministic. If this ValueCounts is empty, returns an empty slice.

func (*ValueCounts) UnmarshalJSON

func (vc *ValueCounts) UnmarshalJSON(data []byte) error

UnmarshalJSON converts a JSON-serialised ValueCounts object, serialised using MarshalJSON, back into a Go object. Existing counts are discarded.

Note that the serialised data is formatted as an array of (value, count) pairs, and it is possible to create an array with multiple counts for the same value. If this occurs, an error will be raised.

If any error is encountered, the original countData object is not modified.

Jump to

Keyboard shortcuts

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