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