PGROUTING  3.2
randomSpanningTree_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: randomSpanningTree_driver.cpp
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2018 Aditya Pratap Singh
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 <sstream>
32 #include <deque>
33 #include <vector>
34 
36 
37 #include "cpp_common/pgr_alloc.hpp"
38 #include "cpp_common/pgr_assert.h"
39 
40 template < class G >
41 static
42 std::vector<pgr_randomSpanningTree_t>
44  G &graph,
45  int64_t root_vertex ) {
46  Pgr_randomSpanningTree< G > fn_randomSpanningTree;
47  return fn_randomSpanningTree.randomSpanningTree(graph, root_vertex);
48 }
49 
50 
51 void
53  pgr_edge_t *data_edges,
54  size_t total_edges,
55  int64_t root_vertex,
56  bool directed,
57  pgr_randomSpanningTree_t **return_tuples,
58  size_t *return_count,
59  char ** log_msg,
60  char ** notice_msg,
61  char ** err_msg) {
62  std::ostringstream log;
63  std::ostringstream err;
64  std::ostringstream notice;
65  try {
66  pgassert(!(*log_msg));
67  pgassert(!(*notice_msg));
68  pgassert(!(*err_msg));
69  pgassert(!(*return_tuples));
70  pgassert(*return_count == 0);
71  pgassert(total_edges != 0);
72 
73  graphType gType = directed? DIRECTED: UNDIRECTED;
74 
75  std::vector<pgr_randomSpanningTree_t> results;
76 
77  if (directed) {
78  log << "Working with directed Graph\n";
79  pgrouting::DirectedGraph digraph(gType);
80  digraph.insert_edges(data_edges, total_edges);
81  results = pgr_randomSpanningTree(
82  digraph,
83  root_vertex);
84  } else {
85  log << "Working with Undirected Graph\n";
86  pgrouting::UndirectedGraph undigraph(gType);
87  undigraph.insert_edges(data_edges, total_edges);
88  results = pgr_randomSpanningTree(
89  undigraph,
90  root_vertex);
91  }
92 
93  auto count = results.size();
94 
95  if (count == 0) {
96  (*return_tuples) = NULL;
97  (*return_count) = 0;
98  notice <<
99  "No paths found";
100  return;
101  }
102 
103  (*return_tuples) = pgr_alloc(count, (*return_tuples));
104  for (size_t i = 0; i < count; i++) {
105  *((*return_tuples) + i) = results[i];
106  }
107  (*return_count) = count;
108 
109  pgassert(*err_msg == NULL);
110  *log_msg = log.str().empty()?
111  *log_msg :
112  pgr_msg(log.str().c_str());
113  *notice_msg = notice.str().empty()?
114  *notice_msg :
115  pgr_msg(notice.str().c_str());
116  } catch (AssertFailedException &except) {
117  (*return_tuples) = pgr_free(*return_tuples);
118  (*return_count) = 0;
119  err << except.what();
120  *err_msg = pgr_msg(err.str().c_str());
121  *log_msg = pgr_msg(log.str().c_str());
122  } catch (std::exception &except) {
123  (*return_tuples) = pgr_free(*return_tuples);
124  (*return_count) = 0;
125  err << except.what();
126  *err_msg = pgr_msg(err.str().c_str());
127  *log_msg = pgr_msg(log.str().c_str());
128  } catch(...) {
129  (*return_tuples) = pgr_free(*return_tuples);
130  (*return_count) = 0;
131  err << "Caught unknown exception!";
132  *err_msg = pgr_msg(err.str().c_str());
133  *log_msg = pgr_msg(log.str().c_str());
134  }
135 }
do_pgr_randomSpanningTree
void do_pgr_randomSpanningTree(pgr_edge_t *data_edges, size_t total_edges, int64_t root_vertex, bool directed, pgr_randomSpanningTree_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: randomSpanningTree_driver.cpp:52
pgr_alloc
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
Pgr_randomSpanningTree::randomSpanningTree
std::vector< pgr_randomSpanningTree_t > randomSpanningTree(G &graph, int64_t root_vertex)
Definition: pgr_randomSpanningTree.hpp:137
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
pgr_randomSpanningTree
static std::vector< pgr_randomSpanningTree_t > pgr_randomSpanningTree(G &graph, int64_t root_vertex)
Definition: randomSpanningTree_driver.cpp:43
randomSpanningTree_driver.h
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
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
graphType
graphType
Definition: graph_enum.h:30
pgr_assert.h
An assert functionality that uses C++ throw().
pgr_randomSpanningTree_t
Definition: pgr_randomSpanningTree_t.h:37
Pgr_randomSpanningTree
Definition: pgr_randomSpanningTree.hpp:47
DIRECTED
@ DIRECTED
Definition: graph_enum.h:30
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
pgr_randomSpanningTree.hpp
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:139