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 ¶
- type Counter
- func (c *Counter[T]) Add(item T) int
- func (c *Counter[T]) All() iter.Seq2[T, int]
- func (c *Counter[T]) Descending() iter.Seq2[T, int]
- func (c *Counter[T]) Get(item T) int
- func (c *Counter[T]) Items() iter.Seq[T]
- func (c *Counter[T]) MostCommon() (item T, count int)
- func (c *Counter[T]) Remove(item T) int
- func (c *Counter[T]) Reset()
- func (c *Counter[T]) Size() int
- func (c *Counter[T]) Sub(item T) int
- func (c *Counter[T]) Sum() int
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 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 ¶
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
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
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
Get returns the count of item, or 0 if it's not yet been seen.
func (*Counter[T]) Items ¶ added in v0.11.0
Items returns an iterator over the items in the Counter, yielding them in a non-deterministic order.
func (*Counter[T]) MostCommon ¶
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 ¶
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.