counter

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2023 License: AGPL-3.0 Imports: 3 Imported by: 0

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.

func Total

func Total[E comparable](ctr map[E]int) int

Total sums up all counts in the counter.

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.

func TopKAlt

func TopKAlt[E comparable](ctr map[E]int, k int) []Entry[E]

TopKAlt is like TopK, but fully generic, as it does not use the standard library heap. (The standard library heap.Pop() returns any instead of a concrete type.)

Jump to

Keyboard shortcuts

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