PGROUTING  3.2
breadthFirstSearch_driver.cpp File Reference
#include "drivers/breadthFirstSearch/breadthFirstSearch_driver.h"
#include <vector>
#include <algorithm>
#include <string>
#include "cpp_common/pgr_alloc.hpp"
#include "cpp_common/pgr_assert.h"
#include "breadthFirstSearch/pgr_breadthFirstSearch.hpp"
Include dependency graph for breadthFirstSearch_driver.cpp:

Go to the source code of this file.

Functions

void do_pgr_breadthFirstSearch (pgr_edge_t *data_edges, size_t total_edges, int64_t *start_vidsArr, size_t size_start_vidsArr, int64_t max_depth, bool directed, pgr_mst_rt **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
 
template<class G >
std::vector< pgr_mst_rtpgr_breadthFirstSearch (G &graph, std::vector< int64_t > sources, int64_t max_depth)
 

Function Documentation

◆ do_pgr_breadthFirstSearch()

void do_pgr_breadthFirstSearch ( pgr_edge_t data_edges,
size_t  total_edges,
int64_t *  start_vidsArr,
size_t  size_start_vidsArr,
int64_t  max_depth,
bool  directed,
pgr_mst_rt **  return_tuples,
size_t *  return_count,
char **  log_msg,
char **  notice_msg,
char **  err_msg 
)

Definition at line 60 of file breadthFirstSearch_driver.cpp.

72  {
73  std::ostringstream log;
74  std::ostringstream err;
75  std::ostringstream notice;
76  try {
77  pgassert(!(*log_msg));
78  pgassert(!(*notice_msg));
79  pgassert(!(*err_msg));
80  pgassert(!(*return_tuples));
81  pgassert(*return_count == 0);
82  pgassert(total_edges != 0);
83 
84  graphType gType = directed? DIRECTED: UNDIRECTED;
85 
86  log << "Inserting vertices into a c++ vector structure";
87  std::vector<int64_t>
88  start_vertices(start_vidsArr, start_vidsArr + size_start_vidsArr);
89 
90  std::vector<pgr_mst_rt> results;
91  if (directed) {
92  log << "Working with directed Graph\n";
93  pgrouting::DirectedGraph digraph(gType);
94  digraph.insert_edges(data_edges, total_edges);
95  results = pgr_breadthFirstSearch(
96  digraph,
97  start_vertices,
98  max_depth);
99 
100  } else {
101  log << "Working with Undirected Graph\n";
102  pgrouting::UndirectedGraph undigraph(gType);
103  undigraph.insert_edges(data_edges, total_edges);
104 
105  results = pgr_breadthFirstSearch(
106  undigraph,
107  start_vertices,
108  max_depth);
109  }
110 
111  auto count = results.size();
112 
113  if (count == 0) {
114  (*return_tuples) = NULL;
115  (*return_count) = 0;
116  notice <<
117  "No traversal found";
118  *log_msg = pgr_msg(notice.str().c_str());
119  return;
120  }
121 
122  (*return_tuples) = pgr_alloc(count, (*return_tuples));
123  log << "\nConverting a set of traversals into the tuples";
124  for (size_t i = 0; i < count; i++) {
125  *((*return_tuples) + i) = results[i];
126  }
127  (*return_count) = count;
128 
129  pgassert(*err_msg == NULL);
130  *log_msg = log.str().empty()?
131  *log_msg :
132  pgr_msg(log.str().c_str());
133  *notice_msg = notice.str().empty()?
134  *notice_msg :
135  pgr_msg(notice.str().c_str());
136  } catch (AssertFailedException &except) {
137  (*return_tuples) = pgr_free(*return_tuples);
138  (*return_count) = 0;
139  err << except.what();
140  *err_msg = pgr_msg(err.str().c_str());
141  *log_msg = pgr_msg(log.str().c_str());
142  } catch (std::exception &except) {
143  (*return_tuples) = pgr_free(*return_tuples);
144  (*return_count) = 0;
145  err << except.what();
146  *err_msg = pgr_msg(err.str().c_str());
147  *log_msg = pgr_msg(log.str().c_str());
148  } catch(...) {
149  (*return_tuples) = pgr_free(*return_tuples);
150  (*return_count) = 0;
151  err << "Caught unknown exception!";
152  *err_msg = pgr_msg(err.str().c_str());
153  *log_msg = pgr_msg(log.str().c_str());
154  }
155 }

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

Referenced by process().

◆ pgr_breadthFirstSearch()

template<class G >
std::vector<pgr_mst_rt> pgr_breadthFirstSearch ( G &  graph,
std::vector< int64_t >  sources,
int64_t  max_depth 
)

Definition at line 43 of file breadthFirstSearch_driver.cpp.

46  {
47  std::sort(sources.begin(), sources.end());
48  sources.erase(
49  std::unique(sources.begin(), sources.end()),
50  sources.end());
51 
52 
54  auto results = fn_breadthFirstSearch.breadthFirstSearch(
55  graph, sources, max_depth);
56  return results;
57 }

References pgrouting::functions::Pgr_breadthFirstSearch< G >::breadthFirstSearch().

Referenced by do_pgr_breadthFirstSearch().

pgr_alloc
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
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
pgr_breadthFirstSearch
std::vector< pgr_mst_rt > pgr_breadthFirstSearch(G &graph, std::vector< int64_t > sources, int64_t max_depth)
Definition: breadthFirstSearch_driver.cpp:43
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
pgrouting::functions::Pgr_breadthFirstSearch
Definition: pgr_breadthFirstSearch.hpp:42
UNDIRECTED
@ UNDIRECTED
Definition: graph_enum.h:30
pgrouting::functions::Pgr_breadthFirstSearch::breadthFirstSearch
std::vector< pgr_mst_rt > breadthFirstSearch(G &graph, std::vector< int64_t > start_vertex, int64_t depth)
Definition: pgr_breadthFirstSearch.hpp:49
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