Skip to content

gofika/weightsystem

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)
}

About

A generic weight system based on Go generics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages