PGROUTING  3.2
depthFirstSearch_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: depthFirstSearch_driver.cpp
3 
4 Generated with Template by:
5 Copyright (c) 2020 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2020 Ashish Kumar
11 ------
12 
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17 
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22 
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 
27  ********************************************************************PGR-GNU*/
28 
30 
31 #include <vector>
32 #include <algorithm>
33 #include <string>
34 
35 #include "cpp_common/pgr_alloc.hpp"
36 #include "cpp_common/pgr_assert.h"
37 
39 
45 /***********************************************************************
46  *
47  * pgr_depthFirstSearch(
48  * edges_sql TEXT,
49  * root_vids ANYARRAY,
50  * max_depth BIGINT DEFAULT 9223372036854775807,
51  * directed BOOLEAN DEFAULT true
52  * );
53  *
54  ***********************************************************************/
55 
71 template < class G >
72 std::vector < pgr_mst_rt >
74  G &graph,
75  std::vector < int64_t > roots,
76  bool directed,
77  int64_t max_depth) {
78  std::sort(roots.begin(), roots.end());
79  roots.erase(
80  std::unique(roots.begin(), roots.end()),
81  roots.end());
82 
84  auto results = fn_depthFirstSearch.depthFirstSearch(graph, roots, directed, max_depth);
85  return results;
86 }
87 
117 void
119  pgr_edge_t *data_edges,
120  size_t total_edges,
121 
122  int64_t *rootsArr,
123  size_t size_rootsArr,
124 
125  bool directed,
126  int64_t max_depth,
127 
128  pgr_mst_rt **return_tuples,
129  size_t *return_count,
130 
131  char ** log_msg,
132  char ** notice_msg,
133  char ** err_msg) {
134  std::ostringstream log;
135  std::ostringstream err;
136  std::ostringstream notice;
137  try {
138  pgassert(!(*log_msg));
139  pgassert(!(*notice_msg));
140  pgassert(!(*err_msg));
141  pgassert(!(*return_tuples));
142  pgassert(*return_count == 0);
143 
144  std::vector < int64_t > roots(rootsArr, rootsArr + size_rootsArr);
145 
146  std::vector < pgr_mst_rt > results;
147 
148  graphType gType = directed ? DIRECTED : UNDIRECTED;
149 
150  if (directed) {
151  pgrouting::DirectedGraph digraph(gType);
152  digraph.insert_edges(data_edges, total_edges);
153 
154  results = pgr_depthFirstSearch(
155  digraph,
156  roots,
157  directed,
158  max_depth);
159  } else {
160  pgrouting::UndirectedGraph undigraph(gType);
161  undigraph.insert_edges(data_edges, total_edges);
162 
163  results = pgr_depthFirstSearch(
164  undigraph,
165  roots,
166  directed,
167  max_depth);
168  }
169 
170  auto count = results.size();
171 
172  if (count == 0) {
173  (*return_tuples) = NULL;
174  (*return_count) = 0;
175  notice << "No traversal found";
176  *log_msg = pgr_msg(notice.str().c_str());
177  return;
178  }
179 
180  (*return_tuples) = pgr_alloc(count, (*return_tuples));
181  for (size_t i = 0; i < count; i++) {
182  *((*return_tuples) + i) = results[i];
183  }
184  (*return_count) = count;
185 
186  pgassert(*err_msg == NULL);
187  *log_msg = log.str().empty()?
188  *log_msg :
189  pgr_msg(log.str().c_str());
190  *notice_msg = notice.str().empty()?
191  *notice_msg :
192  pgr_msg(notice.str().c_str());
193  } catch (AssertFailedException &except) {
194  (*return_tuples) = pgr_free(*return_tuples);
195  (*return_count) = 0;
196  err << except.what();
197  *err_msg = pgr_msg(err.str().c_str());
198  *log_msg = pgr_msg(log.str().c_str());
199  } catch (std::exception &except) {
200  (*return_tuples) = pgr_free(*return_tuples);
201  (*return_count) = 0;
202  err << except.what();
203  *err_msg = pgr_msg(err.str().c_str());
204  *log_msg = pgr_msg(log.str().c_str());
205  } catch(...) {
206  (*return_tuples) = pgr_free(*return_tuples);
207  (*return_count) = 0;
208  err << "Caught unknown exception!";
209  *err_msg = pgr_msg(err.str().c_str());
210  *log_msg = pgr_msg(log.str().c_str());
211  }
212 }
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
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
depthFirstSearch_driver.h
pgr_depthFirstSearch.hpp
The main file which calls the respective boost function.
pgr_depthFirstSearch
std::vector< pgr_mst_rt > pgr_depthFirstSearch(G &graph, std::vector< int64_t > roots, bool directed, int64_t max_depth)
Calls the main function defined in the C++ Header file.
Definition: depthFirstSearch_driver.cpp:73
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_depthFirstSearch
Definition: pgr_depthFirstSearch.hpp:54
graphType
graphType
Definition: graph_enum.h:30
pgr_assert.h
An assert functionality that uses C++ throw().
DIRECTED
@ DIRECTED
Definition: graph_enum.h:30
do_pgr_depthFirstSearch
void do_pgr_depthFirstSearch(pgr_edge_t *data_edges, size_t total_edges, int64_t *rootsArr, size_t size_rootsArr, bool directed, int64_t max_depth, pgr_mst_rt **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Performs exception handling and converts the results to postgres.
Definition: depthFirstSearch_driver.cpp:118
pgrouting::functions::Pgr_depthFirstSearch::depthFirstSearch
std::vector< pgr_mst_rt > depthFirstSearch(G &graph, std::vector< int64_t > roots, bool directed, int64_t max_depth)
depthFirstSearch function
Definition: pgr_depthFirstSearch.hpp:80
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