bag

package
v1.22.25 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2025 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package bag provides multiset data structures for vote collection and counting. This is the canonical bag implementation for all Lux packages. Import from: github.com/luxfi/consensus/types/bag

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bag

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

Bag is a multiset for tracking votes and consensus state. It provides threshold tracking for consensus algorithms.

func New

func New[T comparable]() Bag[T]

New creates an empty bag

func Of

func Of[T comparable](elts ...T) Bag[T]

Of returns a Bag initialized with [elts]

func (*Bag[T]) Add

func (b *Bag[T]) Add(elts ...T)

Add increases the number of times each element has been seen by one.

func (*Bag[T]) AddCount

func (b *Bag[T]) AddCount(elt T, count int)

AddCount increases the number of times the element has been seen by [count]. If [count] <= 0 this is a no-op.

func (*Bag[T]) Clone

func (b *Bag[T]) Clone() Bag[T]

Clone returns a deep copy of the bag

func (*Bag[T]) Count

func (b *Bag[T]) Count(elt T) int

Count returns the number of [elt] in the bag.

func (*Bag[T]) Equals

func (b *Bag[T]) Equals(other Bag[T]) bool

Equals returns true if the bags contain the same elements

func (*Bag[T]) Filter

func (b *Bag[T]) Filter(filterFunc func(T) bool) Bag[T]

Filter returns a bag with the elements of this bag that return true for [filterFunc], along with their counts. For example, if X is in this bag with count 5, and filterFunc(X) returns true, then the returned bag contains X with count 5.

func (*Bag[_]) Len

func (b *Bag[_]) Len() int

Len returns the number of elements in the bag.

func (*Bag[T]) List

func (b *Bag[T]) List() []T

List returns a list of unique elements that have been added. The returned list doesn't have duplicates.

func (*Bag[T]) Mode

func (b *Bag[T]) Mode() (T, int)

Mode returns the most common element in the bag and the count of that element. If there's a tie, any of the tied element may be returned.

func (*Bag[T]) PrefixedString

func (b *Bag[T]) PrefixedString(prefix string) string

PrefixedString returns a string representation with a prefix on each line

func (*Bag[T]) Remove

func (b *Bag[T]) Remove(elt T)

Remove all instances of [elt] from the bag.

func (*Bag[_]) SetThreshold

func (b *Bag[_]) SetThreshold(threshold int)

SetThreshold sets the number of times an element must be added to be contained in the threshold set. This is critical for consensus voting algorithms.

func (*Bag[T]) Split

func (b *Bag[T]) Split(splitFunc func(T) bool) [2]Bag[T]

Split returns: 1. A bag containing the elements of this bag that return false for [splitFunc]. 2. A bag containing the elements of this bag that return true for [splitFunc]. Counts are preserved in the returned bags. For example, if X is in this bag with count 5, and splitFunc(X) is false, then the first returned bag has X in it with count 5.

func (*Bag[_]) String

func (b *Bag[_]) String() string

String returns a string representation of the bag

func (*Bag[T]) Threshold

func (b *Bag[T]) Threshold() set.Set[T]

Threshold returns the elements that have been seen at least threshold times. Used by consensus algorithms to determine which blocks/votes have sufficient support.

Jump to

Keyboard shortcuts

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