Skip to content

Commit

Permalink
Use function rather than closure for heaps to avoid unnecessary heap …
Browse files Browse the repository at this point in the history
…allocation (TheAlgorithms#148)
  • Loading branch information
rhysd authored and kczimm committed Oct 11, 2020
1 parent 66c20b3 commit b5e1d33
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/data_structures/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ where
{
count: usize,
items: Vec<T>,
comparator: Box<dyn Fn(&T, &T) -> bool>,
comparator: fn(&T, &T) -> bool,
}

impl<T> Heap<T>
where
T: Default,
{
pub fn new(comparator: Box<dyn Fn(&T, &T) -> bool>) -> Self {
pub fn new(comparator: fn(&T, &T) -> bool) -> Self {
Self {
count: 0,
// Add a default in the first spot to offset indexes
Expand Down Expand Up @@ -113,8 +113,7 @@ impl MinHeap {
where
T: Default + Ord,
{
let comparator = |a: &T, b: &T| a < b;
Heap::new(Box::new(comparator))
Heap::new(|a, b| a < b)
}
}

Expand All @@ -125,8 +124,7 @@ impl MaxHeap {
where
T: Default + Ord,
{
let comparator = |a: &T, b: &T| a > b;
Heap::new(Box::new(comparator))
Heap::new(|a, b| a > b)
}
}

Expand Down Expand Up @@ -173,7 +171,7 @@ mod tests {

#[test]
fn test_key_heap() {
let mut heap: Heap<Point> = Heap::new(Box::new(|a, b| a.0 < b.0));
let mut heap: Heap<Point> = Heap::new(|a, b| a.0 < b.0);
heap.add(Point(1, 5));
heap.add(Point(3, 10));
heap.add(Point(-2, 4));
Expand Down

0 comments on commit b5e1d33

Please sign in to comment.