PGROUTING  2.6
bdDijkstra_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: bdDijkstra_driver.cpp
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
6 Mail: project@pgrouting.org
7 
8 Function's developer:
9 Copyright (c) 2016 Celia Virginia Vergara Castillo
10 Mail: vicky_vergara@hotmail.com
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 <sstream>
33 #include <deque>
34 #include <vector>
35 #include <algorithm>
36 
38 
39 
40 #include "cpp_common/pgr_alloc.hpp"
41 #include "cpp_common/pgr_assert.h"
42 
44 
45 
46 
47 
48 template < class G >
49 static
50 std::deque<Path>
52  G &graph,
53  std::vector < int64_t > sources,
54  std::vector < int64_t > targets,
55 
56  std::ostream &log,
57  bool only_cost) {
58  log << "entering static function\n";
59  std::sort(sources.begin(), sources.end());
60  sources.erase(
61  std::unique(sources.begin(), sources.end()),
62  sources.end());
63 
64  std::sort(targets.begin(), targets.end());
65  targets.erase(
66  std::unique(targets.begin(), targets.end()),
67  targets.end());
68 
70 
71  std::deque<Path> paths;
72  for (const auto source : sources) {
73  for (const auto target : targets) {
74  fn_bdDijkstra.clear();
75  paths.push_back(fn_bdDijkstra.pgr_bdDijkstra(
76  graph.get_V(source), graph.get_V(target), only_cost));
77  }
78  }
79 
80  log << fn_bdDijkstra.log();
81 
82  return paths;
83 }
84 
85 
86 void
88  pgr_edge_t *data_edges,
89  size_t total_edges,
90  int64_t *start_vidsArr,
91  size_t size_start_vidsArr,
92  int64_t *end_vidsArr,
93  size_t size_end_vidsArr,
94 
95  bool directed,
96  bool only_cost,
97 
98  General_path_element_t **return_tuples,
99  size_t *return_count,
100  char **log_msg,
101  char **notice_msg,
102  char **err_msg) {
103  std::ostringstream log;
104  std::ostringstream err;
105  std::ostringstream notice;
106  try {
107  pgassert(total_edges != 0);
108  pgassert(size_start_vidsArr != 0);
109  pgassert(size_end_vidsArr != 0);
110 
111  pgassert(!(*log_msg));
112  pgassert(!(*notice_msg));
113  pgassert(!(*err_msg));
114  pgassert(!(*return_tuples));
115  pgassert(*return_count == 0);
116 
117  log << "Inserting vertices into a c++ vector structure";
118  std::vector<int64_t>
119  start_vertices(start_vidsArr, start_vidsArr + size_start_vidsArr);
120  std::vector< int64_t >
121  end_vertices(end_vidsArr, end_vidsArr + size_end_vidsArr);
122 
123  graphType gType = directed? DIRECTED: UNDIRECTED;
124 
125  std::deque<Path> paths;
126 
127  log << "starting process\n";
128  if (directed) {
129  log << "Working with directed Graph\n";
130  pgrouting::DirectedGraph digraph(gType);
131  digraph.insert_edges(data_edges, total_edges);
132  paths = pgr_bdDijkstra(digraph,
133  start_vertices,
134  end_vertices,
135  log,
136  only_cost);
137  } else {
138  log << "Working with Undirected Graph\n";
139  pgrouting::UndirectedGraph undigraph(gType);
140  undigraph.insert_edges(data_edges, total_edges);
141  paths = pgr_bdDijkstra(
142  undigraph,
143  start_vertices,
144  end_vertices,
145  log,
146  only_cost);
147  }
148 
149  size_t count(0);
150  count = count_tuples(paths);
151 
152  if (count == 0) {
153  (*return_tuples) = NULL;
154  (*return_count) = 0;
155  notice <<
156  "No paths found";
157  *log_msg = pgr_msg(notice.str().c_str());
158  return;
159  }
160 
161  (*return_tuples) = pgr_alloc(count, (*return_tuples));
162  log << "\nConverting a set of paths into the tuples";
163  (*return_count) = (collapse_paths(return_tuples, paths));
164 
165  pgassert(*err_msg == NULL);
166  *log_msg = log.str().empty()?
167  *log_msg :
168  pgr_msg(log.str().c_str());
169  *notice_msg = notice.str().empty()?
170  *notice_msg :
171  pgr_msg(notice.str().c_str());
172  } catch (AssertFailedException &except) {
173  (*return_tuples) = pgr_free(*return_tuples);
174  (*return_count) = 0;
175  err << except.what();
176  *err_msg = pgr_msg(err.str().c_str());
177  *log_msg = pgr_msg(log.str().c_str());
178  } catch (std::exception &except) {
179  (*return_tuples) = pgr_free(*return_tuples);
180  (*return_count) = 0;
181  err << except.what();
182  *err_msg = pgr_msg(err.str().c_str());
183  *log_msg = pgr_msg(log.str().c_str());
184  } catch(...) {
185  (*return_tuples) = pgr_free(*return_tuples);
186  (*return_count) = 0;
187  err << "Caught unknown exception!";
188  *err_msg = pgr_msg(err.str().c_str());
189  *log_msg = pgr_msg(log.str().c_str());
190  }
191 }
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:126
void insert_edges(const T *edges, int64_t count)
Inserts count edges of type T into the graph.
size_t collapse_paths(General_path_element_t **ret_path, const std::deque< Path > &paths)
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:77
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:81
char * pgr_msg(const std::string &msg)
Definition: pgr_alloc.cpp:30
size_t count_tuples(const std::deque< Path > &paths)
void do_pgr_bdDijkstra(pgr_edge_t *data_edges, size_t total_edges, int64_t *start_vidsArr, size_t size_start_vidsArr, int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, bool only_cost, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
Assertions Handling.
Path pgr_bdDijkstra(V start_vertex, V end_vertex, bool only_cost)
virtual const char * what() const
Definition: pgr_assert.cpp:53
graphType
Definition: graph_enum.h:30
static std::deque< Path > pgr_bdDijkstra(G &graph, std::vector< int64_t > sources, std::vector< int64_t > targets, std::ostream &log, bool only_cost)