Skip to content

Commit

Permalink
bringing hashbrown back
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip committed Nov 21, 2020
1 parent a396a5e commit 4fa2e58
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ serde_derive = { version = "1", default-features = false }
thiserror = { version = "1", optional = true }
merlin = { version = "2", default-features = false }
clear_on_drop = { version = "0.2", default-features = false, features = ["nightly"] }
# hashbrown = { version = "0.9.1", default-features = false }
hashbrown = { version = "0.9.1", default-features = false }

[dev-dependencies]
hex = "0.3"
Expand Down
35 changes: 19 additions & 16 deletions src/r1cs/linear_combination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use alloc::vec::Vec;
use core::iter::FromIterator;
use core::ops::{Add, Mul, Neg, Sub};
use curve25519_dalek::scalar::Scalar;
// #[cfg(feature = "std")]
// use hashbrown::hash_map::HashMap;
#[cfg(feature = "std")]
use hashbrown::hash_map::HashMap;

/// Represents a variable in a constraint system.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -119,22 +119,25 @@ impl LinearCombination {
self.terms
}

// #[cfg(feature = "std")]
// pub fn simplify(self) -> Self {
// // Build hashmap to hold unique variables with their values.
// let mut vars: HashMap<Variable, Scalar> = HashMap::new();
/// Simplify linear combination by taking Variables common across terms and adding their corresponding scalars.
/// Useful when linear combinations become large. Takes ownership of linear combination as this function is useful
/// when memory is limited and the obvious action after this function call will be to free the memory held by the old linear combination
#[cfg(feature = "std")]
pub fn simplify(self) -> Self {
// Build hashmap to hold unique variables with their values.
let mut vars: HashMap<Variable, Scalar> = HashMap::new();

// let terms = self.get_terms();
// for (var, val) in terms {
// *vars.entry(var).or_insert(Scalar::zero()) += val;
// }
let terms = self.get_terms();
for (var, val) in terms {
*vars.entry(var).or_insert(Scalar::zero()) += val;
}

// let mut new_lc_terms = vec![];
// for (var, val) in vars {
// new_lc_terms.push((var, val));
// }
// new_lc_terms.iter().collect()
// }
let mut new_lc_terms = vec![];
for (var, val) in vars {
new_lc_terms.push((var, val));
}
new_lc_terms.iter().collect()
}
}

impl Default for LinearCombination {
Expand Down

0 comments on commit 4fa2e58

Please sign in to comment.