Documentation
¶
Overview ¶
Package weightslice provides a "weighted slice" implementation where the order of elements within the slice changes based on a comparable weight value.
Index ¶
Constants ¶
const ( // Ascending can be passed to New to order elements in ascending order. Ascending uint = iota // Descending can be passed to New to order elements in descending order. Descending )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Slice ¶
The Slice type represents a slice of values that each have an associated weight that modifies their order within the slice. The Slice is sorted on calls to Append and SetWeight.
func New ¶
New returns a new Slice instance that will sort elements by weight in the specified sort order. The slice can be prepopulated with the given "initial" parameter. The sort order can either be Ascending or Descending. For any other value, Ascending is assumed.
func (*Slice[T, W]) Append ¶
func (s *Slice[T, W]) Append(value T, weight W)
Append an element to the Slice with an initial weight.
func (*Slice[T, W]) Range ¶
Range over all elements in the Slice based on their weight. This method uses a copy of the original slice for iteration to avoid concurrency issues.