counter

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package counter implements a convenient data structure for counting comparable values.

Inspired by python's collections.Counter.

The counter is not safe for concurrent access across goroutines, the caller is responsible for synchronising concurrent access.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Counter

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

Counter is a convenient construct for counting comparable values.

func Collect

func Collect[T comparable](items iter.Seq[T]) *Counter[T]

Collect builds a Counter from an iterator of items, counting them in the order they are iterated through.

items := []string{"apple", "apple", "orange", "banana"}
counts := counter.Collect(slices.Values(items))

func From

func From[T comparable](items []T) *Counter[T]

From builds a Counter from an existing slice of items, counting them in the order they are given.

items := []string{"apple", "apple", "orange", "banana"}
counts := counter.From(items)

func New

func New[T comparable]() *Counter[T]

New constructs and returns a new Counter.

func WithCapacity added in v0.15.0

func WithCapacity[T comparable](capacity int) *Counter[T]

WithCapacity constructs and returns a new Counter with the given capacity.

This can be a useful performance improvement when the number of unique items to count is known ahead of time as it eliminates the need for reallocation.

func (*Counter[T]) Add

func (c *Counter[T]) Add(item T) int

Add adds an item to the counter, incrementing it's count and returning the new count.

If the item doesn't exist, it is added to the counter with the count of 1, and 1 will be returned.

func (*Counter[T]) All added in v0.18.0

func (c *Counter[T]) All() iter.Seq2[T, int]

All returns an iterator over the item, count pairs in the Counter, yielding them in a non-deterministic order.

func (*Counter[T]) Descending added in v0.11.0

func (c *Counter[T]) Descending() iter.Seq2[T, int]

Descending returns an iterator of the item, count pairs in the Counter, yielding them in descending order (i.e. highest count first).

func (*Counter[T]) Get added in v0.11.0

func (c *Counter[T]) Get(item T) int

Get returns the count of item, or 0 if it's not yet been seen.

func (*Counter[T]) Items added in v0.11.0

func (c *Counter[T]) Items() iter.Seq[T]

Items returns an iterator over the items in the Counter, yielding them in a non-deterministic order.

func (*Counter[T]) MostCommon

func (c *Counter[T]) MostCommon() (item T, count int)

MostCommon returns the item with the highest count, along with the count itself.

If the Counter is empty it returns the zero value for the item type and 0 for the count.

func (*Counter[T]) Remove

func (c *Counter[T]) Remove(item T) int

Remove completely removes an item from the counter, returning it's count if it was present, or 0 if not.

If the item didn't exist, Remove is a no-op.

func (*Counter[T]) Reset

func (c *Counter[T]) Reset()

Reset resets the Counter, removing all items and freeing the memory.

func (*Counter[T]) Size

func (c *Counter[T]) Size() int

Size returns the current number of items in the Counter.

func (*Counter[T]) Sub

func (c *Counter[T]) Sub(item T) int

Sub subtracts an item from the counter, decrementing it's count and returning the new count.

If the decrement would set the item's count to 0, it is then removed entirely and 0 is returned.

If the item doesn't exist, this is a no-op returning 0.

func (*Counter[T]) Sum

func (c *Counter[T]) Sum() int

Sum returns the sum of all the item counts in the Counter, effectively the overall number of items including duplicates.

Jump to

Keyboard shortcuts

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