weightsystem

package module
v0.0.0-...-b586615 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2024 License: MIT Imports: 2 Imported by: 0

README

codecov Build Status go.dev Go Report Card Licenses

weightsystem

A generic weight system based on Go generics

Basic Usage

Installation

To get the package, execute:

go get github.com/gofika/weightsystem
Example
package main

import (
	"fmt"
	"math/rand/v2"

	"github.com/gofika/weightsystem"
)

func displayWeights(ws *weightsystem.WeightSystem[string]) {
	for item, weight := range ws.Weights() {
		fmt.Printf("Item: %#+v, Weight: %.2f\n", item, weight)
	}
}

func main() {
	ipList := []string{"192.168.1.1", "192.168.1.2", "192.168.1.3"}
	ipSystem := weightsystem.New[string]()
	// Add items to the system
	ipSystem.AddItems(ipList)

	// randomly adjust weights
	for i := 0; i < 5000; i++ {
		ip := ipSystem.GetItem()
		success := rand.Float32() < 0.7
		ipSystem.AdjustWeight(ip, success)
	}

	fmt.Println("Current weights:")
	displayWeights(ipSystem)

	// Add new items to the system with AVG weight
	ipSystem.AddItem("192.168.1.4")

	// Add new items to the system with custom weight
	ipSystem.AddItem("192.168.1.5", weightsystem.WithWeight(150.0))

	// Add new items to the system with custom weight. out of range, will set to minimum weight
	ipSystem.AddItem("192.168.1.6", weightsystem.WithWeight(-50.0))

	fmt.Println("\nWeights after adding new items:")
	displayWeights(ipSystem)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AddItemOption

type AddItemOption func(*AddItemOptions)

func WithWeight

func WithWeight(weight float64) AddItemOption

type AddItemOptions

type AddItemOptions struct {
	// contains filtered or unexported fields
}

type NewOption

type NewOption func(*NewOptions)

func WithMaxWeight

func WithMaxWeight(maxWeight float64) NewOption

func WithMinWeight

func WithMinWeight(minWeight float64) NewOption

type NewOptions

type NewOptions struct {
	// contains filtered or unexported fields
}

type WeightSystem

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

WeightSystem is a generic weight system for any comparable type T

func New

func New[T comparable](opts ...NewOption) *WeightSystem[T]

New creates a new WeightSystem with the given items and weights Options can be passed to set the initial weight, min weight, and max weight If no options are passed, min weight will be 1, and max weight will be 1000

func (*WeightSystem[T]) AddItem

func (ws *WeightSystem[T]) AddItem(item T, opts ...AddItemOption)

AddItem adds a new item to the system with the given weight

func (*WeightSystem[T]) AddItems

func (ws *WeightSystem[T]) AddItems(items []T, opts ...AddItemOption)

AddItems adds multiple items to the system with the same weight

func (*WeightSystem[T]) AdjustWeight

func (ws *WeightSystem[T]) AdjustWeight(item T, success bool)

AdjustWeight adjusts the weight of an item based on success or failure

func (*WeightSystem[T]) GetItem

func (ws *WeightSystem[T]) GetItem() T

GetItem returns a randomly selected item based on weights

func (*WeightSystem[T]) MaxWeight

func (ws *WeightSystem[T]) MaxWeight() float64

MaxWeight returns the maximum weight allowed in the system

func (*WeightSystem[T]) MinWeight

func (ws *WeightSystem[T]) MinWeight() float64

MinWeight returns the minimum weight allowed in the system

func (*WeightSystem[T]) RemoveItem

func (ws *WeightSystem[T]) RemoveItem(item T)

RemoveItem removes an item from the system

func (*WeightSystem[T]) SortedWeights

func (ws *WeightSystem[T]) SortedWeights() []WeightedItem[T]

SortedWeights returns a sorted slice of WeightedItems, from highest weight to lowest

func (*WeightSystem[T]) Weights

func (ws *WeightSystem[T]) Weights() map[T]float64

Weights returns a clone of the current weights map

type WeightedItem

type WeightedItem[T comparable] struct {
	Item   T
	Weight float64
}

WeightedItem represents an item with its associated weight

Jump to

Keyboard shortcuts

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