PGROUTING  3.2
edwardMoore_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: edwardMoore_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 
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
19 
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24 
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 
29 ********************************************************************PGR-GNU*/
30 
32 
33 #include <sstream>
34 #include <deque>
35 #include <vector>
36 #include <algorithm>
37 
38 #include <set>
39 #include <functional>
40 #include <limits>
41 #include <numeric>
42 
44 
45 
46 
47 
50 
51 
52 #include "cpp_common/pgr_alloc.hpp"
53 #include "cpp_common/pgr_assert.h"
54 
55 
56 template < class G >
57 std::deque< Path >
59  G &graph,
60  std::vector <pgr_combination_t> &combinations,
61  std::vector < int64_t > sources,
62  std::vector < int64_t > targets) {
63  std::sort(sources.begin(), sources.end());
64  sources.erase(
65  std::unique(sources.begin(), sources.end()),
66  sources.end());
67 
68  std::sort(targets.begin(), targets.end());
69  targets.erase(
70  std::unique(targets.begin(), targets.end()),
71  targets.end());
72 
74  auto paths = combinations.empty() ?
75  fn_edwardMoore.edwardMoore(graph, sources, targets)
76  : fn_edwardMoore.edwardMoore(graph, combinations);
77 
78  return paths;
79 }
80 
81 void
83  pgr_edge_t *data_edges,
84  size_t total_edges,
85  pgr_combination_t *combinations,
86  size_t total_combinations,
87  int64_t *start_vidsArr,
88  size_t size_start_vidsArr,
89  int64_t *end_vidsArr,
90  size_t size_end_vidsArr,
91  bool directed,
92 
93  General_path_element_t **return_tuples,
94  size_t *return_count,
95  char ** log_msg,
96  char ** notice_msg,
97  char ** err_msg) {
98  std::ostringstream log;
99  std::ostringstream err;
100  std::ostringstream notice;
101 
102  try {
103  pgassert(total_edges != 0);
104  pgassert(!(*log_msg));
105  pgassert(!(*notice_msg));
106  pgassert(!(*err_msg));
107  pgassert(!(*return_tuples));
108  pgassert(*return_count == 0);
109  pgassert(data_edges);
110  pgassert((start_vidsArr && end_vidsArr) || combinations);
111  pgassert((size_start_vidsArr && size_end_vidsArr) || total_combinations);
112 
113  graphType gType = directed? DIRECTED: UNDIRECTED;
114 
115  log << "Inserting vertices into a c++ vector structure";
116  std::vector<int64_t>
117  start_vertices(start_vidsArr, start_vidsArr + size_start_vidsArr);
118  std::vector< int64_t >
119  end_vertices(end_vidsArr, end_vidsArr + size_end_vidsArr);
120  std::vector< pgr_combination_t >
121  combinations_vector(combinations, combinations + total_combinations);
122 
123 
124  std::deque< Path >paths;
125  if (directed) {
126  log << "\nWorking with directed Graph";
127  pgrouting::DirectedGraph digraph(gType);
128  digraph.insert_edges(data_edges, total_edges);
129  paths = pgr_edwardMoore(
130  digraph,
131  combinations_vector,
132  start_vertices,
133  end_vertices);
134  } else {
135  log << "\nWorking with Undirected Graph";
136  pgrouting::UndirectedGraph undigraph(gType);
137  undigraph.insert_edges(data_edges, total_edges);
138 
139  paths = pgr_edwardMoore(
140  undigraph,
141  combinations_vector,
142  start_vertices,
143  end_vertices);
144  }
145 
146  size_t count(0);
147  count = count_tuples(paths);
148 
149  if (count == 0) {
150  (*return_tuples) = NULL;
151  (*return_count) = 0;
152  notice <<
153  "No paths found";
154  *log_msg = pgr_msg(notice.str().c_str());
155  return;
156  }
157 
158  (*return_tuples) = pgr_alloc(count, (*return_tuples));
159  log << "\nConverting a set of paths into the tuples";
160  (*return_count) = (collapse_paths(return_tuples, paths));
161 
162  *log_msg = log.str().empty()?
163  *log_msg :
164  pgr_msg(log.str().c_str());
165  *notice_msg = notice.str().empty()?
166  *notice_msg :
167  pgr_msg(notice.str().c_str());
168  } catch (AssertFailedException &except) {
169  (*return_tuples) = pgr_free(*return_tuples);
170  (*return_count) = 0;
171  err << except.what();
172  *err_msg = pgr_msg(err.str().c_str());
173  *log_msg = pgr_msg(log.str().c_str());
174  } catch (std::exception &except) {
175  (*return_tuples) = pgr_free(*return_tuples);
176  (*return_count) = 0;
177  err << except.what();
178  *err_msg = pgr_msg(err.str().c_str());
179  *log_msg = pgr_msg(log.str().c_str());
180  } catch(...) {
181  (*return_tuples) = pgr_free(*return_tuples);
182  (*return_count) = 0;
183  err << "Caught unknown exception!";
184  *err_msg = pgr_msg(err.str().c_str());
185  *log_msg = pgr_msg(log.str().c_str());
186  }
187 }
pgr_alloc
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
pgr_base_graph.hpp
count_tuples
size_t count_tuples(const std::deque< Path > &paths)
Definition: basePath_SSEC.cpp:387
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
collapse_paths
size_t collapse_paths(General_path_element_t **ret_path, const std::deque< Path > &paths)
Definition: basePath_SSEC.cpp:310
edwardMoore_driver.h
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
pgr_edwardMoore
std::deque< Path > pgr_edwardMoore(G &graph, std::vector< pgr_combination_t > &combinations, std::vector< int64_t > sources, std::vector< int64_t > targets)
Definition: edwardMoore_driver.cpp:58
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_edwardMoore
Definition: pgr_edwardMoore.hpp:41
pgr_combination_t
Definition: pgr_combination_t.h:43
graphType
graphType
Definition: graph_enum.h:30
pgr_assert.h
An assert functionality that uses C++ throw().
DIRECTED
@ DIRECTED
Definition: graph_enum.h:30
General_path_element_t
Definition: general_path_element_t.h:37
pgrouting::functions::Pgr_edwardMoore::edwardMoore
std::deque< Path > edwardMoore(G &graph, std::vector< int64_t > start_vertex, std::vector< int64_t > end_vertex)
Definition: pgr_edwardMoore.hpp:49
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
pgr_edwardMoore.hpp
pgrouting::graph::Pgr_base_graph
Definition: pgr_base_graph.hpp:168
do_pgr_edwardMoore
void do_pgr_edwardMoore(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: edwardMoore_driver.cpp:82
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:139
basePath_SSEC.hpp