Skip to content

Commit

Permalink
make MinHeap more efficient by calling push_heap instead of make_heap
Browse files Browse the repository at this point in the history
  • Loading branch information
varunagrawal committed Nov 7, 2024
1 parent 9666725 commit ae43b2a
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions gtsam/discrete/DecisionTreeFactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,15 @@ namespace gtsam {
/// Push value onto the heap
void push(double x) {
v_.push_back(x);
std::make_heap(v_.begin(), v_.end(), std::greater<double>{});
std::push_heap(v_.begin(), v_.end(), std::greater<double>{});
}

/// Push value `x`, `n` number of times.
void push(double x, size_t n) {
v_.insert(v_.end(), n, x);
std::make_heap(v_.begin(), v_.end(), std::greater<double>{});
for (size_t i = 0; i < n; ++i) {
v_.push_back(x);
std::push_heap(v_.begin(), v_.end(), std::greater<double>{});
}
}

/// Pop the top value of the heap.
Expand All @@ -390,10 +392,11 @@ namespace gtsam {
*/
void print(const std::string& s = "") {
std::cout << (s.empty() ? "" : s + " ");
for (size_t i = 0; i < v_.size() - 1; i++) {
std::cout << v_.at(i) << ",";
for (size_t i = 0; i < v_.size(); i++) {
std::cout << v_.at(i);
if (v_.size() > 1 && i < v_.size() - 1) std::cout << ", ";
}
std::cout << v_.at(v_.size() - 1) << std::endl;
std::cout << std::endl;
}

/// Return true if heap is empty.
Expand Down

0 comments on commit ae43b2a

Please sign in to comment.