Documentation ¶
Overview ¶
Package counter counts occurrences of elements in slices and also returns their k most- or least-frequent elements. It also provides utility functions for working with counters.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Add ¶
func Add[E comparable](a, b map[E]int) map[E]int
Add adds counter a and b together and returns a copy.
func Average ¶ added in v0.1.6
func Average[E constraints.Integer | constraints.Float](ctr map[E]int) float64
func Counter ¶
func Counter[S ~[]E, E comparable](slice S) map[E]int
Counter counts occurrences of each element of the slice and returns a map of elements to their counts.
func Subtract ¶
func Subtract[E comparable](a, b map[E]int) map[E]int
Subtract subtracts the counter b from a and returns a copy.
Types ¶
type Entry ¶
type Entry[E comparable] struct { Element E Count int }
Entry represents an element-count pair.
func BottomK ¶
func BottomK[E comparable](ctr map[E]int, k int) []Entry[E]
BottomK returns the k least-frequent elements from the counter. The returned entries are in ascending order of frequency. If two elements have the same count, their relative order in the returned slice is undefined, however they will be after all elements that occur less frequently.
func TopK ¶
func TopK[E comparable](ctr map[E]int, k int) []Entry[E]
TopK returns the k most-frequent elements from the counter. The returned entries are in descending order of frequency. If two elements have the same count, their relative order in the returned slice is undefined, however they will be after all elements that occur more frequently.