PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ksp_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: ksp_driver.cpp
3 
4 Copyright (c) 2015 Celia Virginia Vergara Castillo
5 vicky_vergara@hotmail.com
6 
7 ------
8 
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23 ********************************************************************PGR-GNU*/
24 
25 
26 #include "./ksp_driver.h"
27 
28 #include <sstream>
29 #include <deque>
30 #include <vector>
31 
32 #include "./pgr_ksp.hpp"
33 
34 #include "./../../common/src/pgr_alloc.hpp"
35 #include "./../../common/src/pgr_assert.h"
36 #include "./../../common/src/pgr_types.h"
37 
38 #include "./../../common/src/pgr_base_graph.hpp"
39 
40 
41 
43  pgr_edge_t *data_edges,
44  size_t total_edges,
45  int64_t start_vid,
46  int64_t end_vid,
47  int k,
48  bool directed,
49  bool heap_paths,
50  General_path_element_t **return_tuples,
51  size_t *return_count,
52  char ** log_msg,
53  char ** notice_msg,
54  char ** err_msg) {
55  std::ostringstream err;
56  std::ostringstream log;
57  std::ostringstream notice;
58  try {
59  pgassert(!(*log_msg));
60  pgassert(!(*notice_msg));
61  pgassert(!(*err_msg));
62  pgassert(!(*return_tuples));
63  pgassert(*return_count == 0);
64  pgassert(total_edges != 0);
65  log << "buuu";
66 
67  graphType gType = directed? DIRECTED: UNDIRECTED;
68 
69  std::deque< Path > paths;
70 
71  if (directed) {
72  pgrouting::DirectedGraph digraph(gType);
74  digraph.insert_edges(data_edges, total_edges);
75  paths = fn_yen.Yen(digraph, start_vid, end_vid, k, heap_paths);
76  } else {
77  pgrouting::UndirectedGraph undigraph(gType);
79  undigraph.insert_edges(data_edges, total_edges);
80  paths = fn_yen.Yen(undigraph, start_vid, end_vid, k, heap_paths);
81  }
82 
83 
84  auto count(count_tuples(paths));
85 
86  if (!(count == 0)) {
87  // get the space required to store all the paths
88  *return_tuples = NULL;
89  *return_tuples = pgr_alloc(count, (*return_tuples));
90 
91  size_t sequence = 0;
92  int route_id = 0;
93  for (const auto &path : paths) {
94  if (path.size() > 0)
95  path.get_pg_ksp_path(return_tuples, sequence, route_id);
96  ++route_id;
97  }
98  }
99  *return_count = count;
100 
101  pgassert(*err_msg == NULL);
102  *log_msg = log.str().empty()?
103  nullptr :
104  strdup(log.str().c_str());
105  *notice_msg = notice.str().empty()?
106  nullptr :
107  strdup(notice.str().c_str());
108  } catch (AssertFailedException &except) {
109  if (*return_tuples) free(*return_tuples);
110  (*return_count) = 0;
111  err << except.what();
112  *err_msg = strdup(err.str().c_str());
113  *log_msg = strdup(log.str().c_str());
114  } catch (std::exception& except) {
115  if (*return_tuples) free(*return_tuples);
116  (*return_count) = 0;
117  err << except.what();
118  *err_msg = strdup(err.str().c_str());
119  *log_msg = strdup(log.str().c_str());
120  } catch(...) {
121  if (*return_tuples) free(*return_tuples);
122  (*return_count) = 0;
123  err << "Caught unknown exception!";
124  *err_msg = strdup(err.str().c_str());
125  *log_msg = strdup(log.str().c_str());
126  }
127 }
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.
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:81
graphType
Definition: pgr_types.h:222
void do_pgr_ksp(pgr_edge_t *data_edges, size_t total_edges, int64_t start_vid, int64_t end_vid, int k, bool directed, bool heap_paths, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: ksp_driver.cpp:42
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
std::deque< Path > Yen(G &graph, int64_t source, int64_t target, int K, bool heap_paths)
Definition: pgr_ksp.cpp:51
char * err_msg
Definition: BDATester.cpp:50