PGROUTING  3.2
breadthFirstSearch_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: breadthFirstSearch_driver.cpp
3 
4 Generated with Template by:
5 Copyright (c) 2019 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2019 Gudesa Venkata Sai Akhil
11 
12 ------
13 
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18 
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23 
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 
28 ********************************************************************PGR-GNU*/
29 
31 
32 #include <vector>
33 #include <algorithm>
34 #include <string>
35 
36 #include "cpp_common/pgr_alloc.hpp"
37 #include "cpp_common/pgr_assert.h"
38 
40 
41 template < class G >
42 std::vector<pgr_mst_rt>
44  G &graph,
45  std::vector < int64_t > sources,
46  int64_t max_depth) {
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 }
58 
59 void
61  pgr_edge_t *data_edges,
62  size_t total_edges,
63  int64_t *start_vidsArr,
64  size_t size_start_vidsArr,
65  int64_t max_depth,
66  bool directed,
67 
68  pgr_mst_rt **return_tuples,
69  size_t *return_count,
70  char ** log_msg,
71  char ** notice_msg,
72  char ** err_msg) {
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 }
pgr_breadthFirstSearch.hpp
pgr_alloc
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
pgr_mst_rt
Definition: pgr_mst_rt.h:36
pgr_edge_t
Definition: pgr_edge_t.h:37
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
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: breadthFirstSearch_driver.cpp:60
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::graph::Pgr_base_graph::insert_edges
void insert_edges(const T *edges, size_t count)
Inserts count edges of type T into the graph.
Definition: pgr_base_graph.hpp:357
pgr_alloc.hpp
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
pgr_assert.h
An assert functionality that uses C++ throw().
DIRECTED
@ DIRECTED
Definition: graph_enum.h:30
breadthFirstSearch_driver.h
pgrouting::alphashape::G
graph::Pgr_base_graph< BG, XY_vertex, Basic_edge > G
Definition: pgr_alphaShape.h:56
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