PGROUTING  2.6
dijkstra_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: many_to_many_dijkstra_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) 2015 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 #include "cpp_common/pgr_alloc.hpp"
40 #include "cpp_common/pgr_assert.h"
41 
42 template < class G >
43 std::deque< Path >
45  G &graph,
46  std::vector < int64_t > sources,
47  std::vector < int64_t > targets,
48  bool only_cost,
49  bool normal) {
50  std::sort(sources.begin(), sources.end());
51  sources.erase(
52  std::unique(sources.begin(), sources.end()),
53  sources.end());
54 
55  std::sort(targets.begin(), targets.end());
56  targets.erase(
57  std::unique(targets.begin(), targets.end()),
58  targets.end());
59 
60  Pgr_dijkstra< G > fn_dijkstra;
61  auto paths = fn_dijkstra.dijkstra(graph, sources, targets, only_cost);
62 
63  if (!normal) {
64  for (auto &path : paths) {
65  path.reverse();
66  }
67  }
68  return paths;
69 }
70 
71 
72 
73 // CREATE OR REPLACE FUNCTION pgr_dijkstra(
74 // sql text,
75 // start_vids anyarray,
76 // end_vids anyarray,
77 // directed boolean default true,
78 void
80  pgr_edge_t *data_edges,
81  size_t total_edges,
82  int64_t *start_vidsArr,
83  size_t size_start_vidsArr,
84  int64_t *end_vidsArr,
85  size_t size_end_vidsArr,
86  bool directed,
87  bool only_cost,
88  bool normal,
89  General_path_element_t **return_tuples,
90  size_t *return_count,
91  char ** log_msg,
92  char ** notice_msg,
93  char ** err_msg) {
94  std::ostringstream log;
95  std::ostringstream err;
96  std::ostringstream notice;
97 
98  try {
99  pgassert(total_edges != 0);
100  pgassert(!(*log_msg));
101  pgassert(!(*notice_msg));
102  pgassert(!(*err_msg));
103  pgassert(!(*return_tuples));
104  pgassert(*return_count == 0);
105 
106  graphType gType = directed? DIRECTED: UNDIRECTED;
107 
108  log << "Inserting vertices into a c++ vector structure";
109  std::vector<int64_t>
110  start_vertices(start_vidsArr, start_vidsArr + size_start_vidsArr);
111  std::vector< int64_t >
112  end_vertices(end_vidsArr, end_vidsArr + size_end_vidsArr);
113 
114  std::deque< Path >paths;
115  if (directed) {
116  log << "\nWorking with directed Graph";
117  pgrouting::DirectedGraph digraph(gType);
118  digraph.insert_edges(data_edges, total_edges);
119  paths = pgr_dijkstra(
120  digraph,
121  start_vertices, end_vertices,
122  only_cost, normal);
123  } else {
124  log << "\nWorking with Undirected Graph";
125  pgrouting::UndirectedGraph undigraph(gType);
126  undigraph.insert_edges(data_edges, total_edges);
127  paths = pgr_dijkstra(
128  undigraph,
129  start_vertices, end_vertices,
130  only_cost, normal);
131  }
132 
133  size_t count(0);
134  count = count_tuples(paths);
135 
136  if (count == 0) {
137  (*return_tuples) = NULL;
138  (*return_count) = 0;
139  notice <<
140  "No paths found";
141  *log_msg = pgr_msg(notice.str().c_str());
142  return;
143  }
144 
145  (*return_tuples) = pgr_alloc(count, (*return_tuples));
146  log << "\nConverting a set of paths into the tuples";
147  (*return_count) = (collapse_paths(return_tuples, paths));
148 
149  *log_msg = log.str().empty()?
150  *log_msg :
151  pgr_msg(log.str().c_str());
152  *notice_msg = notice.str().empty()?
153  *notice_msg :
154  pgr_msg(notice.str().c_str());
155  } catch (AssertFailedException &except) {
156  (*return_tuples) = pgr_free(*return_tuples);
157  (*return_count) = 0;
158  err << except.what();
159  *err_msg = pgr_msg(err.str().c_str());
160  *log_msg = pgr_msg(log.str().c_str());
161  } catch (std::exception &except) {
162  (*return_tuples) = pgr_free(*return_tuples);
163  (*return_count) = 0;
164  err << except.what();
165  *err_msg = pgr_msg(err.str().c_str());
166  *log_msg = pgr_msg(log.str().c_str());
167  } catch(...) {
168  (*return_tuples) = pgr_free(*return_tuples);
169  (*return_count) = 0;
170  err << "Caught unknown exception!";
171  *err_msg = pgr_msg(err.str().c_str());
172  *log_msg = pgr_msg(log.str().c_str());
173  }
174 }
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)
std::deque< Path > pgr_dijkstra(G &graph, std::vector< int64_t > sources, std::vector< int64_t > targets, bool only_cost, bool normal)
void do_pgr_many_to_many_dijkstra(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, bool normal, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:77
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:81
void reverse()
Path dijkstra(G &graph, int64_t start_vertex, int64_t end_vertex, bool only_cost=false)
Dijkstra 1 to 1.
char * pgr_msg(const std::string &msg)
Definition: pgr_alloc.cpp:30
size_t count_tuples(const std::deque< Path > &paths)
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
Assertions Handling.
virtual const char * what() const
Definition: pgr_assert.cpp:53
graphType
Definition: graph_enum.h:30