Skip to content

Commit

Permalink
Add hashmap_with_capacity (#313)
Browse files Browse the repository at this point in the history
* add hash_map_with_capacity

* rm _
  • Loading branch information
cospectrum authored Apr 24, 2024
1 parent 556a6b3 commit c11d03d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/extract.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::cmp::Ordering;
use std::fmt::Debug;

use crate::util::HashMap;
use crate::util::{hashmap_with_capacity, HashMap};
use crate::{Analysis, EClass, EGraph, Id, Language, RecExpr};

/** Extracting a single [`RecExpr`] from an [`EGraph`].
Expand Down Expand Up @@ -134,8 +134,9 @@ pub trait CostFunction<L: Language> {
/// down the [`RecExpr`].
///
fn cost_rec(&mut self, expr: &RecExpr<L>) -> Self::Cost {
let mut costs: HashMap<Id, Self::Cost> = HashMap::default();
for (i, node) in expr.as_ref().iter().enumerate() {
let nodes = expr.as_ref();
let mut costs = hashmap_with_capacity::<Id, Self::Cost>(nodes.len());
for (i, node) in nodes.iter().enumerate() {
let cost = self.cost(node, |i| costs[&i].clone());
costs.insert(Id::from(i), cost);
}
Expand Down
2 changes: 1 addition & 1 deletion src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ impl<L: Language> RecExpr<L> {
}

pub(crate) fn compact(mut self) -> Self {
let mut ids = HashMap::<Id, Id>::default();
let mut ids = hashmap_with_capacity::<Id, Id>(self.nodes.len());
let mut set = IndexSet::default();
for (i, node) in self.nodes.drain(..).enumerate() {
let node = node.map_children(|id| ids[&id]);
Expand Down
4 changes: 4 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ mod hashmap {
pub(crate) type HashSet<K> = hashbrown::HashSet<K, BuildHasher>;
}

pub(crate) fn hashmap_with_capacity<K, V>(cap: usize) -> hashmap::HashMap<K, V> {
hashmap::HashMap::with_capacity_and_hasher(cap, <_>::default())
}

pub(crate) type IndexMap<K, V> = indexmap::IndexMap<K, V, BuildHasher>;
pub(crate) type IndexSet<K> = indexmap::IndexSet<K, BuildHasher>;

Expand Down

0 comments on commit c11d03d

Please sign in to comment.