PGROUTING  3.2
bellman_ford_driver.cpp File Reference
#include "drivers/bellman_ford/bellman_ford_driver.h"
#include <sstream>
#include <deque>
#include <vector>
#include <algorithm>
#include <string>
#include "bellman_ford/pgr_bellman_ford.hpp"
#include "cpp_common/pgr_alloc.hpp"
#include "cpp_common/pgr_assert.h"
Include dependency graph for bellman_ford_driver.cpp:

Go to the source code of this file.

Functions

void do_pgr_bellman_ford (pgr_edge_t *data_edges, size_t total_edges, pgr_combination_t *combinations, size_t total_combinations, int64_t *start_vidsArr, size_t size_start_vidsArr, int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, bool only_cost, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
 
template<class G >
std::deque< Pathpgr_bellman_ford (G &graph, std::vector< pgr_combination_t > &combinations, std::vector< int64_t > sources, std::vector< int64_t > targets, std::string &log, bool only_cost=false)
 

Function Documentation

◆ do_pgr_bellman_ford()

void do_pgr_bellman_ford ( pgr_edge_t data_edges,
size_t  total_edges,
pgr_combination_t combinations,
size_t  total_combinations,
int64_t *  start_vidsArr,
size_t  size_start_vidsArr,
int64_t *  end_vidsArr,
size_t  size_end_vidsArr,
bool  directed,
bool  only_cost,
General_path_element_t **  return_tuples,
size_t *  return_count,
char **  log_msg,
char **  notice_msg,
char **  err_msg 
)

Definition at line 83 of file bellman_ford_driver.cpp.

99  {
100  std::ostringstream log;
101  std::ostringstream err;
102  std::ostringstream notice;
103  try {
104  pgassert(!(*log_msg));
105  pgassert(!(*notice_msg));
106  pgassert(!(*err_msg));
107  pgassert(!(*return_tuples));
108  pgassert(*return_count == 0);
109  pgassert(total_edges != 0);
110  pgassert(data_edges);
111  pgassert((start_vidsArr && end_vidsArr) || combinations);
112  pgassert((size_start_vidsArr && size_end_vidsArr) || total_combinations);
113 
114  graphType gType = directed? DIRECTED: UNDIRECTED;
115 
116  log << "Inserting vertices into a c++ vector structure";
117  std::vector<int64_t>
118  start_vertices(start_vidsArr, start_vidsArr + size_start_vidsArr);
119  std::vector< int64_t >
120  end_vertices(end_vidsArr, end_vidsArr + size_end_vidsArr);
121  std::vector< pgr_combination_t >
122  combinations_vector(combinations, combinations + total_combinations);
123 
124  std::deque< Path >paths;
125  std::string logstr;
126  if (directed) {
127  log << "Working with directed Graph\n";
128  pgrouting::DirectedGraph digraph(gType);
129  digraph.insert_edges(data_edges, total_edges);
130  paths = pgr_bellman_ford(digraph,
131  combinations_vector,
132  start_vertices,
133  end_vertices,
134  logstr,
135  only_cost);
136  } else {
137  log << "Working with Undirected Graph\n";
138  pgrouting::UndirectedGraph undigraph(gType);
139  undigraph.insert_edges(data_edges, total_edges);
140  paths = pgr_bellman_ford(
141  undigraph,
142  combinations_vector,
143  start_vertices,
144  end_vertices,
145  logstr,
146  only_cost);
147  }
148  log<< logstr;
149  size_t count(0);
150  count = count_tuples(paths);
151 
152  if (count == 0) {
153  (*return_tuples) = NULL;
154  (*return_count) = 0;
155  notice <<
156  "No paths found";
157  *log_msg = pgr_msg(notice.str().c_str());
158  return;
159  }
160 
161  (*return_tuples) = pgr_alloc(count, (*return_tuples));
162  log << "\nConverting a set of paths into the tuples";
163  (*return_count) = (collapse_paths(return_tuples, paths));
164 
165  *log_msg = log.str().empty()?
166  *log_msg :
167  pgr_msg(log.str().c_str());
168  *notice_msg = notice.str().empty()?
169  *notice_msg :
170  pgr_msg(notice.str().c_str());
171  } catch (AssertFailedException &except) {
172  (*return_tuples) = pgr_free(*return_tuples);
173  (*return_count) = 0;
174  err << except.what();
175  *err_msg = pgr_msg(err.str().c_str());
176  *log_msg = pgr_msg(log.str().c_str());
177  } catch (std::exception &except) {
178  (*return_tuples) = pgr_free(*return_tuples);
179  (*return_count) = 0;
180  err << except.what();
181  *err_msg = pgr_msg(err.str().c_str());
182  *log_msg = pgr_msg(log.str().c_str());
183  } catch(...) {
184  (*return_tuples) = pgr_free(*return_tuples);
185  (*return_count) = 0;
186  err << "Caught unknown exception!";
187  *err_msg = pgr_msg(err.str().c_str());
188  *log_msg = pgr_msg(log.str().c_str());
189  }
190 }

References collapse_paths(), count_tuples(), DIRECTED, pgrouting::graph::Pgr_base_graph< G, T_V, T_E >::insert_edges(), pgassert, pgr_alloc(), pgr_bellman_ford(), pgr_free(), pgr_msg(), UNDIRECTED, and AssertFailedException::what().

Referenced by process().

◆ pgr_bellman_ford()

template<class G >
std::deque< Path > pgr_bellman_ford ( G &  graph,
std::vector< pgr_combination_t > &  combinations,
std::vector< int64_t >  sources,
std::vector< int64_t >  targets,
std::string &  log,
bool  only_cost = false 
)

Definition at line 54 of file bellman_ford_driver.cpp.

60  {
61  std::sort(sources.begin(), sources.end());
62  sources.erase(
63  std::unique(sources.begin(), sources.end()),
64  sources.end());
65 
66  std::sort(targets.begin(), targets.end());
67  targets.erase(
68  std::unique(targets.begin(), targets.end()),
69  targets.end());
70 
71  Pgr_bellman_ford< G > fn_bellman_ford;
72  auto paths = combinations.empty() ?
73  fn_bellman_ford.bellman_ford(graph, sources, targets, only_cost)
74  : fn_bellman_ford.bellman_ford(graph, combinations, only_cost);
75  log += fn_bellman_ford.get_log();
76  for (auto &p : paths) {
77  p.recalculate_agg_cost();
78  }
79  return paths;
80 }

References Pgr_bellman_ford< G >::bellman_ford(), and pgrouting::Pgr_messages::get_log().

Referenced by do_pgr_bellman_ford().

pgr_alloc
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
count_tuples
size_t count_tuples(const std::deque< Path > &paths)
Definition: basePath_SSEC.cpp:387
pgr_msg
char * pgr_msg(const std::string &msg)
Definition: pgr_alloc.cpp:30
AssertFailedException::what
virtual const char * what() const
Definition: pgr_assert.cpp:67
collapse_paths
size_t collapse_paths(General_path_element_t **ret_path, const std::deque< Path > &paths)
Definition: basePath_SSEC.cpp:310
Pgr_bellman_ford::bellman_ford
Path bellman_ford(G &graph, int64_t start_vertex, int64_t end_vertex, bool only_cost=false)
BellmanFord 1 to 1.
Definition: pgr_bellman_ford.hpp:62
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
UNDIRECTED
@ UNDIRECTED
Definition: graph_enum.h:30
Pgr_bellman_ford
Definition: pgr_bellman_ford.hpp:53
pgr_bellman_ford
std::deque< Path > pgr_bellman_ford(G &graph, std::vector< pgr_combination_t > &combinations, std::vector< int64_t > sources, std::vector< int64_t > targets, std::string &log, bool only_cost=false)
Definition: bellman_ford_driver.cpp:54
pgrouting::Pgr_messages::get_log
std::string get_log() const
get_log
Definition: pgr_messages.cpp:36
graphType
graphType
Definition: graph_enum.h:30
DIRECTED
@ DIRECTED
Definition: graph_enum.h:30
pgr_free
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:77
pgrouting::graph::Pgr_base_graph
Definition: pgr_base_graph.hpp:168
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:139