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

Go to the source code of this file.

Functions

template<class G >
bool costCheck (G &graph)
 
void do_pgr_binaryBreadthFirstSearch (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, 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_binaryBreadthFirstSearch (G &graph, std::vector< pgr_combination_t > &combinations, std::vector< int64_t > sources, std::vector< int64_t > targets)
 

Variables

const char COST_ERR_MSG []
 
const size_t MAX_UNIQUE_EDGE_COSTS = 2
 

Function Documentation

◆ costCheck()

template<class G >
bool costCheck ( G &  graph)

Definition at line 75 of file binaryBreadthFirstSearch_driver.cpp.

75  {
76  typedef typename G::E E;
77  typedef typename G::E_i E_i;
78 
79  auto edges = boost::edges(graph.graph);
80  E e;
81  E_i out_i;
82  E_i out_end;
83  std::set<double> cost_set;
84  for (boost::tie(out_i, out_end) = edges;
85  out_i != out_end; ++out_i) {
86  e = *out_i;
87  cost_set.insert(graph[e].cost);
88 
89  if (cost_set.size() > MAX_UNIQUE_EDGE_COSTS) {
90  return false;
91  }
92  }
93 
94  if (cost_set.size() == 2) {
95  if (*cost_set.begin() != 0.0) {
96  return false;
97  }
98  }
99 
100  return true;
101 }

References MAX_UNIQUE_EDGE_COSTS.

Referenced by do_pgr_binaryBreadthFirstSearch().

◆ do_pgr_binaryBreadthFirstSearch()

void do_pgr_binaryBreadthFirstSearch ( 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,
General_path_element_t **  return_tuples,
size_t *  return_count,
char **  log_msg,
char **  notice_msg,
char **  err_msg 
)

Definition at line 106 of file binaryBreadthFirstSearch_driver.cpp.

121  {
122  std::ostringstream log;
123  std::ostringstream err;
124  std::ostringstream notice;
125 
126  try {
127  pgassert(total_edges != 0);
128  pgassert(!(*log_msg));
129  pgassert(!(*notice_msg));
130  pgassert(!(*err_msg));
131  pgassert(!(*return_tuples));
132  pgassert(*return_count == 0);
133  pgassert(data_edges);
134  pgassert((start_vidsArr && end_vidsArr) || combinations);
135  pgassert((size_start_vidsArr && size_end_vidsArr) || total_combinations);
136 
137  graphType gType = directed? DIRECTED: UNDIRECTED;
138 
139  // log << "Inserting vertices into a c++ vector structure";
140  std::vector<int64_t>
141  start_vertices(start_vidsArr, start_vidsArr + size_start_vidsArr);
142  std::vector< int64_t >
143  end_vertices(end_vidsArr, end_vidsArr + size_end_vidsArr);
144  std::vector< pgr_combination_t >
145  combinations_vector(combinations, combinations + total_combinations);
146 
147  std::deque< Path >paths;
148  if (directed) {
149  // log << "\nWorking with directed Graph";
150  pgrouting::DirectedGraph digraph(gType);
151  digraph.insert_edges(data_edges, total_edges);
152 
153  if (!(costCheck(digraph))) {
154  err << COST_ERR_MSG;
155  *err_msg = pgr_msg(err.str().c_str());
156  return;
157  }
159  digraph,
160  combinations_vector,
161  start_vertices,
162  end_vertices);
163 
164  } else {
165  // log << "\nWorking with Undirected Graph";
166  pgrouting::UndirectedGraph undigraph(gType);
167  undigraph.insert_edges(data_edges, total_edges);
168 
169  if (!(costCheck(undigraph))) {
170  err << COST_ERR_MSG;
171  *err_msg = pgr_msg(err.str().c_str());
172  return;
173  }
174 
176  undigraph,
177  combinations_vector,
178  start_vertices,
179  end_vertices);
180  }
181 
182  size_t count(0);
183  count = count_tuples(paths);
184 
185  if (count == 0) {
186  (*return_tuples) = NULL;
187  (*return_count) = 0;
188  notice <<
189  "No paths found";
190  *log_msg = pgr_msg(notice.str().c_str());
191  return;
192  }
193 
194  (*return_tuples) = pgr_alloc(count, (*return_tuples));
195  log << "\nConverting a set of paths into the tuples";
196  (*return_count) = (collapse_paths(return_tuples, paths));
197 
198  *log_msg = log.str().empty()?
199  *log_msg :
200  pgr_msg(log.str().c_str());
201  *notice_msg = notice.str().empty()?
202  *notice_msg :
203  pgr_msg(notice.str().c_str());
204  } catch (AssertFailedException &except) {
205  (*return_tuples) = pgr_free(*return_tuples);
206  (*return_count) = 0;
207  err << except.what();
208  *err_msg = pgr_msg(err.str().c_str());
209  *log_msg = pgr_msg(log.str().c_str());
210  } catch (std::exception &except) {
211  (*return_tuples) = pgr_free(*return_tuples);
212  (*return_count) = 0;
213  err << except.what();
214  *err_msg = pgr_msg(err.str().c_str());
215  *log_msg = pgr_msg(log.str().c_str());
216  } catch(...) {
217  (*return_tuples) = pgr_free(*return_tuples);
218  (*return_count) = 0;
219  err << "Caught unknown exception!";
220  *err_msg = pgr_msg(err.str().c_str());
221  *log_msg = pgr_msg(log.str().c_str());
222  }
223 }

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

Referenced by process().

◆ pgr_binaryBreadthFirstSearch()

template<class G >
std::deque< Path > pgr_binaryBreadthFirstSearch ( G &  graph,
std::vector< pgr_combination_t > &  combinations,
std::vector< int64_t >  sources,
std::vector< int64_t >  targets 
)

Definition at line 47 of file binaryBreadthFirstSearch_driver.cpp.

51  {
52  std::sort(sources.begin(), sources.end());
53  sources.erase(
54  std::unique(sources.begin(), sources.end()),
55  sources.end());
56 
57  std::sort(targets.begin(), targets.end());
58  targets.erase(
59  std::unique(targets.begin(), targets.end()),
60  targets.end());
61 
63  auto paths = combinations.empty() ?
64  fn_binaryBreadthFirstSearch.binaryBreadthFirstSearch(graph, sources, targets)
65  : fn_binaryBreadthFirstSearch.binaryBreadthFirstSearch(graph, combinations);
66 
67  return paths;
68 }

References pgrouting::functions::Pgr_binaryBreadthFirstSearch< G >::binaryBreadthFirstSearch().

Referenced by do_pgr_binaryBreadthFirstSearch().

Variable Documentation

◆ COST_ERR_MSG

const char COST_ERR_MSG[]
Initial value:
= "Graph Condition Failed: Graph should have atmost two distinct non-negative edge costs! "
"If there are exactly two distinct edge costs, one of them must equal zero!"

Definition at line 71 of file binaryBreadthFirstSearch_driver.cpp.

Referenced by do_pgr_binaryBreadthFirstSearch().

◆ MAX_UNIQUE_EDGE_COSTS

const size_t MAX_UNIQUE_EDGE_COSTS = 2

Definition at line 70 of file binaryBreadthFirstSearch_driver.cpp.

Referenced by costCheck().

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
pgrouting::functions::Pgr_binaryBreadthFirstSearch
Definition: pgr_binaryBreadthFirstSearch.hpp:44
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
pgrouting::functions::Pgr_binaryBreadthFirstSearch::binaryBreadthFirstSearch
std::deque< Path > binaryBreadthFirstSearch(G &graph, std::vector< int64_t > start_vertex, std::vector< int64_t > end_vertex)
Definition: pgr_binaryBreadthFirstSearch.hpp:52
UNDIRECTED
@ UNDIRECTED
Definition: graph_enum.h:30
pgr_binaryBreadthFirstSearch
std::deque< Path > pgr_binaryBreadthFirstSearch(G &graph, std::vector< pgr_combination_t > &combinations, std::vector< int64_t > sources, std::vector< int64_t > targets)
Definition: binaryBreadthFirstSearch_driver.cpp:47
pgrouting::alphashape::E
boost::graph_traits< BG >::edge_descriptor E
Definition: pgr_alphaShape.h:57
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
MAX_UNIQUE_EDGE_COSTS
const size_t MAX_UNIQUE_EDGE_COSTS
Definition: binaryBreadthFirstSearch_driver.cpp:70
COST_ERR_MSG
const char COST_ERR_MSG[]
Definition: binaryBreadthFirstSearch_driver.cpp:71
pgrouting::graph::Pgr_base_graph
Definition: pgr_base_graph.hpp:168
costCheck
bool costCheck(G &graph)
Definition: binaryBreadthFirstSearch_driver.cpp:75
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:139