Documentation
¶
Overview ¶
Package counter implements a Counter for comparable items.
Index ¶
- type Counter
- func (c *Counter[T]) Add(item T) int
- func (c *Counter[T]) Clone() *Counter[T]
- func (c *Counter[T]) Contains(item T) bool
- func (c *Counter[T]) Get(item T) int
- func (c *Counter[T]) Items() []T
- func (c *Counter[T]) Iter() iter.Seq2[T, int]
- func (c *Counter[T]) Len() int
- func (c *Counter[T]) Map() map[T]int
- func (c *Counter[T]) MostCommon(n int) []ItemCount[T]
- func (c *Counter[T]) Remove(item T) bool
- func (c *Counter[T]) String() string
- func (c *Counter[T]) Sub(item T) int
- func (c *Counter[T]) Total() int
- func (c *Counter[T]) Update(items ...T)
- type ItemCount
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 }
A Counter keeps track of the counts of items (which must be comparable).
func FromMap ¶ added in v0.4.0
func FromMap[T comparable](m map[T]int) *Counter[T]
FromMap returns a new Counter initialized from a map.
func (*Counter[T]) Contains ¶
Contains returns true if the item is in the Counter (even when the count is 0).
func (*Counter[T]) Items ¶
func (c *Counter[T]) Items() []T
Items returns a slice with items where each item is repeated according to their count. Items with negative count are ignored.
func (*Counter[T]) Iter ¶ added in v0.5.0
Iter returns an iterator over item-count pairs. It is sorted in descending order by count.
func (*Counter[T]) MostCommon ¶
MostCommon returns n items orderd by count (descending); n <= 0: all items.
type ItemCount ¶
type ItemCount[T comparable] struct { Item T Count int }
An ItemCount contains an item and its count.
Click to show internal directories.
Click to hide internal directories.