weight_matchers

Crates.ioweight_matchers
lib.rsweight_matchers
version0.6.0
created_at2025-01-08 22:30:14.080012+00
updated_at2025-05-04 11:49:00.112786+00
descriptionEfficiently find items by matching weight. You can build the lookup structure at compile time.
homepage
repositoryhttps://sourceforge.net/p/weight-matchers/
max_upload_size
id1509240
size152,313
(daniel-pfeiffer)

documentation

README

Efficiently find items by matching weight. If your data is static, you can build the lookup structure (a complete binary tree) at compile time.

You can use any inferred numeric type for the weights. You can have any range for the lookup, by default 0.0 .. 1.0 for floats, and their respective whole spectrum MIN ..= MAX for integers.

As a small example, let’s make red more than twice as likely as green, and that in turn five times as likely as blue.

# extern crate weight_matchers;
# use weight_matchers::{dyn_weights, weights};
# fn rand_f32() -> f32 { 0.0 }
let colours = weights! {
    0.70 => "red",
    0.25 => "green",
    0.05 => "blue",
};

// If you have dynamic values, use this macro instead:
let frequent = 0.70;
let most = "red";
let dyn_colours = dyn_weights! {
    frequent => most,
    0.25 => "green",
    0.05 => "blue",
};
// Any random source of the same type and range as your weights will do.
println!("I got {}", colours.get(rand_f32()));
println!("I got {}", dyn_colours.get(rand_f32()));

There’s a blog about the design choices behind this.

Commit count: 0

cargo fmt