weighted_list/lib.rs
1//! A list implementation for weighted randomisation.
2//!
3//! This crate provides the [`WeightedList<V,W>`](WeightedList) struct, which stores values each with assigned weights. When randomly selecting items, those with a higher weight are more likely to be chosen.
4//!
5//! ## Example
6//!
7//! ```
8//! use weighted_list::*;
9//!
10//! let wl = WeightedList::<String, u8>::from([
11//! (2, "sup".to_string()),
12//! (3, "nova".to_string()),
13//! (5, "shard".to_string()),
14//! ]);
15//!
16//! for item in &wl {
17//! println!("{} has weight {}", item.value, item.weight);
18//! }
19//!
20//! if let Some(result) = wl.select_random_value(&mut rand::rng()) {
21//! println!("{result}");
22//! }
23//! ```
24//!
25//! ## Why might you need this?
26//!
27//! - Weighted randomisation for a reward system
28//! - Item stacking for an inventory system
29//! - Statistical sampling
30//!
31//! For more detailed guidance on how to use the struct, see [`WeightedList`].
32
33mod root;
34pub use root::{ Weight };
35
36mod weighted_item;
37pub use weighted_item::{ WeightedItem, WItem };
38
39mod weighted_list;
40pub use weighted_list::{ WeightedList, WList };
41
42
43#[cfg(feature = "frozen")] mod frozen_weighted_item;
44#[cfg(feature = "frozen")] pub use frozen_weighted_item::{ FrozenWeightedItem, FWItem };
45
46#[cfg(feature = "frozen")] mod frozen_weighted_list;
47#[cfg(feature = "frozen")] pub use frozen_weighted_list::{ FrozenWeightedList, FWList };