Skip to content

Commit

Permalink
CoinNodeHeap: Replace pointer arrays by std::vector
Browse files Browse the repository at this point in the history
  • Loading branch information
a-andre committed Aug 1, 2024
1 parent 0dbad72 commit 91848d1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 19 deletions.
20 changes: 3 additions & 17 deletions src/CoinNodeHeap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,17 @@
// position of the first child node in vector
#define childPos(node) ((node * 2) + 1)

static void *xmalloc( const size_t size );

CoinNodeHeap::CoinNodeHeap(size_t numNodes) {
#ifdef DEBUGCG
assert(numNodes > 0);
#endif
numNodes_ = numNodes;
pq_ = (std::pair<size_t, double>*)xmalloc(sizeof(std::pair<size_t, double>) * numNodes_);
pos_ = (size_t*)xmalloc(sizeof(size_t) * numNodes_);
pq_ = std::vector<std::pair<size_t, double> >(numNodes);
pos_ = std::vector<size_t>(numNodes);
reset();
}

CoinNodeHeap::~CoinNodeHeap() {
free(pq_);
free(pos_);
}
CoinNodeHeap::~CoinNodeHeap() {}

void CoinNodeHeap::reset() {
for (size_t i = 0; i < numNodes_; i++) {
Expand Down Expand Up @@ -111,12 +106,3 @@ bool CoinNodeHeap::isEmpty() const {
return (pq_[0].second >= NODEHEAP_INFTY);
}

static void *xmalloc( const size_t size ) {
void *result = malloc( size );
if (!result) {
fprintf(stderr, "No more memory available. Trying to allocate %zu bytes.", size);
abort();
}

return result;
}
5 changes: 3 additions & 2 deletions src/CoinNodeHeap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "CoinUtilsConfig.h"
#include <cstddef>
#include <utility>
#include <vector>

/**
* Monotone heap.
Expand Down Expand Up @@ -68,12 +69,12 @@ class COINUTILSLIB_EXPORT CoinNodeHeap {
/**
* Priority queue itself
**/
std::pair<size_t, double> *pq_;
std::vector<std::pair<size_t, double> > pq_;

/**
* Indicates the position of each node in pq
**/
size_t *pos_;
std::vector<size_t> pos_;

/**
* Number of nodes of the heap.
Expand Down

0 comments on commit 91848d1

Please sign in to comment.