Skip to content

Commit

Permalink
refactor(*): make generic over T: FastFloat
Browse files Browse the repository at this point in the history
  • Loading branch information
david-r-cox committed Jan 22, 2024
1 parent 79370ed commit 9009fee
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 121 deletions.
13 changes: 6 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 20 additions & 24 deletions Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ args@{
ignoreLockHash,
}:
let
nixifiedLockHash = "075e4b694f77989ee77e009093dce14f1e4c228918d29e08fac7490cc9b5eb54";
nixifiedLockHash = "0aa7eb3e470c717b11b3122a9f2b45ede5734e5a31839691217d1dde47c0ee93";
workspaceSrc = if args.workspaceSrc == null then ./. else args.workspaceSrc;
currentLockHash = builtins.hashFile "sha256" (workspaceSrc + /Cargo.lock);
lockHashIgnored = if ignoreLockHash
Expand Down Expand Up @@ -87,8 +87,6 @@ in
registry = "registry+https://github.com/rust-lang/crates.io-index";
src = fetchCratesIo { inherit name version; sha256 = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"; };
features = builtins.concatLists [
[ "default" ]
[ "perf-literal" ]
[ "std" ]
];
dependencies = {
Expand Down Expand Up @@ -388,7 +386,7 @@ in
];
dependencies = {
${ if rootFeatures' ? "mps/clap" || rootFeatures' ? "mps/default" then "heck" else null } = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".heck."0.4.1" { inherit profileName; }).out;
${ if rootFeatures' ? "mps/clap" || rootFeatures' ? "mps/default" then "proc_macro2" else null } = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".proc-macro2."1.0.76" { inherit profileName; }).out;
${ if rootFeatures' ? "mps/clap" || rootFeatures' ? "mps/default" then "proc_macro2" else null } = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".proc-macro2."1.0.78" { inherit profileName; }).out;
${ if rootFeatures' ? "mps/clap" || rootFeatures' ? "mps/default" then "quote" else null } = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".quote."1.0.35" { inherit profileName; }).out;
${ if rootFeatures' ? "mps/clap" || rootFeatures' ? "mps/default" then "syn" else null } = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".syn."2.0.48" { inherit profileName; }).out;
};
Expand Down Expand Up @@ -482,7 +480,7 @@ in
oorandom = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".oorandom."11.1.3" { inherit profileName; }).out;
plotters = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".plotters."0.3.5" { inherit profileName; }).out;
rayon = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".rayon."1.8.1" { inherit profileName; }).out;
regex = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".regex."1.10.2" { inherit profileName; }).out;
regex = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".regex."1.10.3" { inherit profileName; }).out;
serde = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".serde."1.0.195" { inherit profileName; }).out;
serde_derive = (buildRustPackages."registry+https://github.com/rust-lang/crates.io-index".serde_derive."1.0.195" { profileName = "__noProfile"; }).out;
serde_json = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".serde_json."1.0.111" { inherit profileName; }).out;
Expand Down Expand Up @@ -837,7 +835,6 @@ in
src = fetchCratesIo { inherit name version; sha256 = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"; };
features = builtins.concatLists [
[ "alloc" ]
[ "default" ]
[ "std" ]
[ "use_std" ]
];
Expand Down Expand Up @@ -884,7 +881,6 @@ in
nom = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".nom."7.1.3" { inherit profileName; }).out;
nom_tracable = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".nom-tracable."0.9.1" { inherit profileName; }).out;
nom_locate = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".nom_locate."4.2.0" { inherit profileName; }).out;
num_traits = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".num-traits."0.2.17" { inherit profileName; }).out;
serde = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".serde."1.0.195" { inherit profileName; }).out;
};
devDependencies = {
Expand Down Expand Up @@ -1074,11 +1070,11 @@ in
];
});

"registry+https://github.com/rust-lang/crates.io-index".proc-macro2."1.0.76" = overridableMkRustCrate (profileName: rec {
"registry+https://github.com/rust-lang/crates.io-index".proc-macro2."1.0.78" = overridableMkRustCrate (profileName: rec {
name = "proc-macro2";
version = "1.0.76";
version = "1.0.78";
registry = "registry+https://github.com/rust-lang/crates.io-index";
src = fetchCratesIo { inherit name version; sha256 = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c"; };
src = fetchCratesIo { inherit name version; sha256 = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae"; };
features = builtins.concatLists [
[ "default" ]
[ "proc-macro" ]
Expand Down Expand Up @@ -1137,7 +1133,7 @@ in
[ "proc-macro" ]
];
dependencies = {
proc_macro2 = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".proc-macro2."1.0.76" { inherit profileName; }).out;
proc_macro2 = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".proc-macro2."1.0.78" { inherit profileName; }).out;
};
});

Expand Down Expand Up @@ -1231,27 +1227,27 @@ in
};
});

"registry+https://github.com/rust-lang/crates.io-index".regex."1.10.2" = overridableMkRustCrate (profileName: rec {
"registry+https://github.com/rust-lang/crates.io-index".regex."1.10.3" = overridableMkRustCrate (profileName: rec {
name = "regex";
version = "1.10.2";
version = "1.10.3";
registry = "registry+https://github.com/rust-lang/crates.io-index";
src = fetchCratesIo { inherit name version; sha256 = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343"; };
src = fetchCratesIo { inherit name version; sha256 = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15"; };
features = builtins.concatLists [
[ "std" ]
];
dependencies = {
aho_corasick = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".aho-corasick."1.1.2" { inherit profileName; }).out;
memchr = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".memchr."2.7.1" { inherit profileName; }).out;
regex_automata = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".regex-automata."0.4.3" { inherit profileName; }).out;
regex_automata = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".regex-automata."0.4.4" { inherit profileName; }).out;
regex_syntax = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".regex-syntax."0.8.2" { inherit profileName; }).out;
};
});

"registry+https://github.com/rust-lang/crates.io-index".regex-automata."0.4.3" = overridableMkRustCrate (profileName: rec {
"registry+https://github.com/rust-lang/crates.io-index".regex-automata."0.4.4" = overridableMkRustCrate (profileName: rec {
name = "regex-automata";
version = "0.4.3";
version = "0.4.4";
registry = "registry+https://github.com/rust-lang/crates.io-index";
src = fetchCratesIo { inherit name version; sha256 = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f"; };
src = fetchCratesIo { inherit name version; sha256 = "3b7fa1134405e2ec9353fd416b17f8dacd46c473d7d3fd1cf202706a14eb792a"; };
features = builtins.concatLists [
[ "alloc" ]
[ "meta" ]
Expand Down Expand Up @@ -1375,7 +1371,7 @@ in
[ "default" ]
];
dependencies = {
proc_macro2 = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".proc-macro2."1.0.76" { inherit profileName; }).out;
proc_macro2 = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".proc-macro2."1.0.78" { inherit profileName; }).out;
quote = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".quote."1.0.35" { inherit profileName; }).out;
syn = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".syn."2.0.48" { inherit profileName; }).out;
};
Expand Down Expand Up @@ -1444,7 +1440,7 @@ in
[ "quote" ]
];
dependencies = {
proc_macro2 = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".proc-macro2."1.0.76" { inherit profileName; }).out;
proc_macro2 = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".proc-macro2."1.0.78" { inherit profileName; }).out;
quote = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".quote."1.0.35" { inherit profileName; }).out;
unicode_ident = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".unicode-ident."1.0.12" { inherit profileName; }).out;
};
Expand All @@ -1467,7 +1463,7 @@ in
[ "visit" ]
];
dependencies = {
proc_macro2 = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".proc-macro2."1.0.76" { inherit profileName; }).out;
proc_macro2 = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".proc-macro2."1.0.78" { inherit profileName; }).out;
quote = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".quote."1.0.35" { inherit profileName; }).out;
unicode_ident = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".unicode-ident."1.0.12" { inherit profileName; }).out;
};
Expand Down Expand Up @@ -1673,7 +1669,7 @@ in
bumpalo = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".bumpalo."3.14.0" { inherit profileName; }).out;
log = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".log."0.4.20" { inherit profileName; }).out;
once_cell = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".once_cell."1.19.0" { inherit profileName; }).out;
proc_macro2 = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".proc-macro2."1.0.76" { inherit profileName; }).out;
proc_macro2 = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".proc-macro2."1.0.78" { inherit profileName; }).out;
quote = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".quote."1.0.35" { inherit profileName; }).out;
syn = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".syn."2.0.48" { inherit profileName; }).out;
wasm_bindgen_shared = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".wasm-bindgen-shared."0.2.90" { inherit profileName; }).out;
Expand Down Expand Up @@ -1703,7 +1699,7 @@ in
[ "spans" ]
];
dependencies = {
proc_macro2 = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".proc-macro2."1.0.76" { inherit profileName; }).out;
proc_macro2 = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".proc-macro2."1.0.78" { inherit profileName; }).out;
quote = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".quote."1.0.35" { inherit profileName; }).out;
syn = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".syn."2.0.48" { inherit profileName; }).out;
wasm_bindgen_backend = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".wasm-bindgen-backend."0.2.90" { inherit profileName; }).out;
Expand Down Expand Up @@ -1912,7 +1908,7 @@ in
registry = "registry+https://github.com/rust-lang/crates.io-index";
src = fetchCratesIo { inherit name version; sha256 = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6"; };
dependencies = {
proc_macro2 = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".proc-macro2."1.0.76" { inherit profileName; }).out;
proc_macro2 = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".proc-macro2."1.0.78" { inherit profileName; }).out;
quote = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".quote."1.0.35" { inherit profileName; }).out;
syn = (rustPackages."registry+https://github.com/rust-lang/crates.io-index".syn."2.0.48" { inherit profileName; }).out;
};
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ hashbrown = "0.14.3"
nom = "7.1.3"
nom-tracable = "0.9.0"
nom_locate = "4.2.0"
num-traits = "0.2.17"
serde = { version = "1.0.195", features = ["serde_derive"], optional = true }

[dev-dependencies]
Expand Down
13 changes: 7 additions & 6 deletions src/model/bounds_map.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use crate::types::{BoundType, Bounds};
use color_eyre::{eyre::eyre, Result};
use fast_float::FastFloat;
use hashbrown::{HashMap, HashSet};

#[derive(Debug, Default, Clone, PartialEq)]
pub struct BoundsMap(
HashMap<String, HashMap<(String, BoundType), Option<f32>>>,
pub struct BoundsMap<T: FastFloat>(
HashMap<String, HashMap<(String, BoundType), Option<T>>>,
);

impl TryFrom<(&Bounds<'_, f32>, &HashSet<&str>)> for BoundsMap {
impl<T: FastFloat> TryFrom<(&Bounds<'_, T>, &HashSet<&str>)> for BoundsMap<T> {
type Error = color_eyre::Report;

fn try_from(t: (&Bounds<'_, f32>, &HashSet<&str>)) -> Result<Self> {
fn try_from(t: (&Bounds<'_, T>, &HashSet<&str>)) -> Result<Self> {
let mut bounds = BoundsMap(HashMap::new());
let (bounds_lines, column_names) = t;
for b in bounds_lines {
Expand All @@ -31,13 +32,13 @@ impl TryFrom<(&Bounds<'_, f32>, &HashSet<&str>)> for BoundsMap {
}
}

impl BoundsMap {
impl<T: FastFloat> BoundsMap<T> {
fn insert(
&mut self,
bound_name: &str,
column_name: &str,
bound_type: BoundType,
value: Option<f32>,
value: Option<T>,
) -> Result<()> {
match self.0.get_mut(bound_name.trim()) {
None => {
Expand Down
15 changes: 8 additions & 7 deletions src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@ use crate::model::row_column_value_map::RowColumnValueMap;
use crate::model::row_type_map::RowTypeMap;
use crate::types::Parser;
use color_eyre::Result;
use fast_float::FastFloat;
use hashbrown::HashSet;

#[derive(Debug, Default, Clone, PartialEq)]
pub struct Model {
pub struct Model<T: FastFloat> {
pub name: String,
pub row_types: RowTypeMap,
pub values: RowColumnValueMap,
pub rhs: RhsMap,
pub bounds: BoundsMap,
pub ranges: RangesMap,
pub values: RowColumnValueMap<T>,
pub rhs: RhsMap<T>,
pub bounds: BoundsMap<T>,
pub ranges: RangesMap<T>,
}

impl TryFrom<Parser<'_, f32>> for Model {
impl<T: FastFloat> TryFrom<Parser<'_, T>> for Model<T> {
type Error = color_eyre::Report;

fn try_from(parsed: Parser<f32>) -> Result<Self> {
fn try_from(parsed: Parser<T>) -> Result<Self> {
let row_types = RowTypeMap::try_from(&parsed.rows)?;
let values = RowColumnValueMap::try_from((&parsed.columns, &row_types))?;
let rhs = match parsed.rhs {
Expand Down
11 changes: 6 additions & 5 deletions src/model/ranges_map.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use crate::model::row_type_map::RowTypeMap;
use crate::types::Ranges;
use color_eyre::{eyre::eyre, Result};
use fast_float::FastFloat;
use hashbrown::HashMap;

#[derive(Debug, Default, Clone, PartialEq)]
pub struct RangesMap(HashMap<String, HashMap<String, f32>>);
pub struct RangesMap<T: FastFloat>(HashMap<String, HashMap<String, T>>);

impl TryFrom<(&Ranges<'_, f32>, &RowTypeMap)> for RangesMap {
impl<T: FastFloat> TryFrom<(&Ranges<'_, T>, &RowTypeMap)> for RangesMap<T> {
type Error = color_eyre::Report;

fn try_from(t: (&Ranges<'_, f32>, &RowTypeMap)) -> Result<Self> {
fn try_from(t: (&Ranges<'_, T>, &RowTypeMap)) -> Result<Self> {
let mut ranges = RangesMap(HashMap::new());
let (ranges_lines, row_types) = t;
for r in ranges_lines {
Expand All @@ -24,12 +25,12 @@ impl TryFrom<(&Ranges<'_, f32>, &RowTypeMap)> for RangesMap {
}
}

impl RangesMap {
impl<T: FastFloat> RangesMap<T> {
fn insert(
&mut self,
ranges_name: &str,
row_name: &str,
value: f32,
value: T,
) -> Result<()> {
match self.0.get_mut(ranges_name) {
None => {
Expand Down
16 changes: 6 additions & 10 deletions src/model/rhs_map.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use crate::model::row_type_map::RowTypeMap;
use crate::types::Rhs;
use color_eyre::{eyre::eyre, Result};
use fast_float::FastFloat;
use hashbrown::HashMap;

#[derive(Debug, Default, Clone, PartialEq)]
pub struct RhsMap(HashMap<String, HashMap<String, f32>>);
pub struct RhsMap<T: FastFloat>(HashMap<String, HashMap<String, T>>);

impl TryFrom<(&Rhs<'_, f32>, &RowTypeMap)> for RhsMap {
impl<T: FastFloat> TryFrom<(&Rhs<'_, T>, &RowTypeMap)> for RhsMap<T> {
type Error = color_eyre::Report;

fn try_from(t: (&Rhs<'_, f32>, &RowTypeMap)) -> Result<Self> {
fn try_from(t: (&Rhs<'_, T>, &RowTypeMap)) -> Result<Self> {
let mut rhs = RhsMap(HashMap::new());
let (rhs_lines, row_types) = t;
for r in rhs_lines {
Expand All @@ -24,13 +25,8 @@ impl TryFrom<(&Rhs<'_, f32>, &RowTypeMap)> for RhsMap {
}
}

impl RhsMap {
fn insert(
&mut self,
rhs_name: &str,
row_name: &str,
value: f32,
) -> Result<()> {
impl<T: FastFloat> RhsMap<T> {
fn insert(&mut self, rhs_name: &str, row_name: &str, value: T) -> Result<()> {
match self.0.get_mut(rhs_name) {
None => {
let mut rhs = HashMap::new();
Expand Down
13 changes: 8 additions & 5 deletions src/model/row_column_value_map.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use crate::model::row_type_map::RowTypeMap;
use crate::types::Columns;
use color_eyre::{eyre::eyre, Result};
use fast_float::FastFloat;
use hashbrown::HashMap;

#[derive(Debug, Default, Clone, PartialEq)]
pub struct RowColumnValueMap(HashMap<(String, String), f32>);
pub struct RowColumnValueMap<T: FastFloat>(HashMap<(String, String), T>);

impl TryFrom<(&Columns<'_, f32>, &RowTypeMap)> for RowColumnValueMap {
impl<T: FastFloat> TryFrom<(&Columns<'_, T>, &RowTypeMap)>
for RowColumnValueMap<T>
{
type Error = color_eyre::Report;

fn try_from(t: (&Columns<'_, f32>, &RowTypeMap)) -> Result<Self> {
fn try_from(t: (&Columns<'_, T>, &RowTypeMap)) -> Result<Self> {
let mut row_column_values = RowColumnValueMap(HashMap::new());
let (columns_lines, row_types) = t;
for c in columns_lines {
Expand All @@ -32,12 +35,12 @@ impl TryFrom<(&Columns<'_, f32>, &RowTypeMap)> for RowColumnValueMap {
}
}

impl RowColumnValueMap {
impl<T: FastFloat> RowColumnValueMap<T> {
fn insert(
&mut self,
row_name: &str,
column_name: &str,
value: f32,
value: T,
) -> Result<()> {
match self.0.insert((row_name.to_string(), column_name.to_string()), value)
{
Expand Down
Loading

0 comments on commit 9009fee

Please sign in to comment.