Skip to content

Commit

Permalink
CoinShortestPath: Replace pointer arrays by std::vector
Browse files Browse the repository at this point in the history
  • Loading branch information
a-andre committed Aug 21, 2024
1 parent f43973f commit 8addcac
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 50 deletions.
49 changes: 16 additions & 33 deletions src/CoinShortestPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,29 @@
#define SPATH_INFTY_NODE std::numeric_limits<size_t>::max()
#define SPATH_INFTY_DIST std::numeric_limits<double>::max()

static void *xmalloc( const size_t size );

CoinShortestPath::CoinShortestPath(size_t nodes, size_t arcs, const size_t *arcStart, const size_t *toNode, const double *dist) {
nodes_ = nodes;
arcs_ = arcs;
nh_ = new CoinNodeHeap(nodes_);
previous_ = (size_t*)xmalloc(sizeof(size_t) * nodes_);
dist_ = (double*)xmalloc(sizeof(double) * nodes_);
path_ = (size_t*)xmalloc(sizeof(size_t) * nodes_);
neighs_ = (std::pair<size_t, double>*)xmalloc(sizeof(std::pair<size_t, double>) * arcs_);
startn_ = (std::pair<size_t, double>**)xmalloc(sizeof(std::pair<size_t, double>*) * (nodes_ + 1));

for (size_t idx = 0; idx < arcs_; idx++) {
neighs_[idx].first = toNode[idx];
neighs_[idx].second = dist[idx];
previous_ = std::vector<size_t>(nodes_);
dist_ = std::vector<double>(nodes_);
path_ = std::vector<size_t>(nodes_);
neighs_ = std::vector<std::vector<std::pair<size_t, double> > >(nodes_);

size_t idx = 0;
for (size_t n = 0; n < nodes_; n++) {
neighs_[n] = std::vector<std::pair<size_t, double> >(arcStart[n+1] - arcStart[n]);
for (size_t i = 0; i < neighs_[n].size(); ++i) {
neighs_[n][i].first = toNode[idx];
neighs_[n][i].second = dist[idx];
#ifdef DEBUGCG
assert(neighs_[idx].first < nodes);
assert(neighs_[n][i].first < nodes);
#endif
}

for (size_t n = 0; n <= nodes_; n++) {
startn_[n] = neighs_ + arcStart[n];
idx++;
}
}
}

CoinShortestPath::~CoinShortestPath() {
free(neighs_);
free(startn_);
free(previous_);
free(dist_);
free(path_);
delete nh_;
}

Expand All @@ -83,7 +75,7 @@ void CoinShortestPath::find(const size_t origin) {
assert(topCost + SPATH_EPS <= SPATH_INFTY_DIST);
#endif
// updating neighbors distances by iterating in all neighbors
for (std::pair<size_t, double> *n = startn_[topNode]; n < startn_[topNode+1]; n++) {
for (std::vector<std::pair<size_t, double> >::iterator n = neighs_[topNode].begin(); n != neighs_[topNode].end(); ++n) {
const size_t toNode = n->first;
const double dist = n->second;
const double newDist = topCost + dist;
Expand Down Expand Up @@ -124,7 +116,7 @@ void CoinShortestPath::find(const size_t origin, const size_t destination) {
break;
}
// updating neighbors distances by iterating in all neighbors
for (std::pair<size_t, double> *n = startn_[topNode]; n < startn_[topNode+1]; n++) {
for (std::vector<std::pair<size_t, double> >::iterator n = neighs_[topNode].begin(); n != neighs_[topNode].end(); ++n) {
const size_t toNode = n->first;
const double dist = n->second;
const double newDist = topCost + dist;
Expand Down Expand Up @@ -170,12 +162,3 @@ size_t CoinShortestPath::previous(size_t node) const {
return previous_[node];
}

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;
}
24 changes: 7 additions & 17 deletions src/CoinShortestPath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "CoinUtilsConfig.h"
#include <cstddef>
#include <utility>
#include <vector>

class CoinNodeHeap;

Expand Down Expand Up @@ -79,7 +80,7 @@ class COINUTILSLIB_EXPORT CoinShortestPath {
/**
* Return the number of arcs in the graph.
**/
size_t numArcs() const { return arcs_; }
size_t numArcs() const { return neighs_.size(); }

/**
* Return the length of the shortest path
Expand All @@ -99,45 +100,34 @@ class COINUTILSLIB_EXPORT CoinShortestPath {
* nodes which should be steped to arrive
* at a given node.
**/
const size_t* previous() const { return previous_; }
const size_t* previous() const { return previous_.data(); }

private:
/**
* Number of nodes in the graph.
**/
size_t nodes_;

/**
* Number of arcs in the graph.
**/
size_t arcs_;

/**
* Array containing all neighbors
**/
std::pair<size_t, double> *neighs_;

/**
* Start of neighbors for node i. The
* neighbor ends at startn[i+1].
**/
std::pair<size_t, double> **startn_;
std::vector<std::vector<std::pair<size_t, double> > > neighs_;

/**
* Length of the shortest paths.
**/
double *dist_;
std::vector<double> dist_;

/**
* Array indexes with all previous nodes which
* should be steped to arrive at a given node.
**/
size_t *previous_;
std::vector<size_t> previous_;

/**
* Temporary storage for the shortest paths.
**/
size_t *path_;
std::vector<size_t> path_;

/**
* Monotone heap used in Dijkstra's algorithm.
Expand Down

0 comments on commit 8addcac

Please sign in to comment.