From 5ad04f14279504788cf77110faafa8d6d4e82119 Mon Sep 17 00:00:00 2001 From: Ben Aaron Goldberg Date: Sun, 24 Oct 2021 04:57:57 -0400 Subject: [PATCH] lib: fixed pn selection so you get the same one on the day Signed-off-by: Ben Aaron Goldberg --- src/lib.rs | 2 +- src/weighted_table.rs | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2a01deb..6333f3d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -127,7 +127,7 @@ impl Default for InstanceSettings { /// she/her or she/her/hers. /// /// Methods are provided to quickly generate these shorter forms, as well as example sentences. -#[derive(Clone, Serialize, Deserialize, PartialEq, Eq, Hash, Debug)] +#[derive(Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] #[serde(from = "[String; 5]", into = "[String; 5]")] pub struct Pronoun { diff --git a/src/weighted_table.rs b/src/weighted_table.rs index fec5740..dd03535 100644 --- a/src/weighted_table.rs +++ b/src/weighted_table.rs @@ -3,7 +3,9 @@ use crate::{ util::{pcg64, pcg64_seed}, }; -use std::collections::HashMap; +use std::{collections::BTreeMap, hash::Hash}; +use std::fmt::Debug; +use std::cmp::Eq; use time::{Date, Month, OffsetDateTime}; @@ -25,9 +27,9 @@ pub const COVID_EPOCH: Date = match Date::from_calendar_date(2020, Month::Januar /// pronoun. Additional methods are provided to perform this random selection on a weighted list, /// using as a seed both an arbitrary string of bytes and a Date. #[derive(Clone, PartialEq, Eq, Debug)] -pub struct WeightedTable(HashMap); +pub struct WeightedTable(BTreeMap); -impl WeightedTable { +impl WeightedTable { /// Attempt to generate a new table /// @@ -40,7 +42,7 @@ impl WeightedTable { /// error if the safety check fails. The original tabel can be accessed as a /// [`HashMap`] via the [`WeightedTable::weights()`] method pub fn new(table: impl IntoIterator) -> Result { - let table: HashMap = table.into_iter() + let table: BTreeMap = table.into_iter() .filter(|(_, weight)| *weight > 0) .collect(); @@ -54,7 +56,7 @@ impl WeightedTable { /// Access the orginal table of weights underlying this table /// /// Returns a [`HashMap`] mapping pronouns to their respective weights. - pub fn weights(&self) -> &HashMap { + pub fn weights(&self) -> &BTreeMap { &self.0 } @@ -101,7 +103,7 @@ impl WeightedTable { ).to_le_bytes() ); new_seed.extend(seed); - self.select(seed.as_ref()) + self.select(&new_seed) } /// Randomly select a pronoun set for a given seed @@ -124,3 +126,6 @@ impl WeightedTable { } } + + +