weightslice

package
v0.0.0-...-953786a Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: MIT Imports: 4 Imported by: 0

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

View Source
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

type Slice[T any, W cmp.Ordered] struct {
	// contains filtered or unexported fields
}

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

func New[T any, W cmp.Ordered](initial []T, sort uint) *Slice[T, W]

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

func (s *Slice[T, W]) Range() iter.Seq2[int, T]

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.

func (*Slice[T, W]) Reset

func (s *Slice[T, W]) Reset()

Reset all weights within the slice to their zero value.

func (*Slice[T, W]) SetWeight

func (s *Slice[T, W]) SetWeight(id int, weight W)

SetWeight modifies the weight value of an element by its id. The provided identifier is internal and only provided to the consumer as the first value when using Range.

Jump to

Keyboard shortcuts

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