PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
dijkstra.hpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 
3 Copyright (c) 2015 pgRouting developers
4 Mail: project@pgrouting.org
5 
6 ------
7 
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 
22 ********************************************************************PGR-GNU*/
23 
24 #include <deque>
25 #include <string>
26 #include <vector>
27 
28 template <typename G>
29 void process_dijkstra(G &graph, const std::vector<std::string> &tokens) {
30  std::string::size_type sz;
31  if (tokens[1].compare("from") != 0) {
32  std::cout << "missing 'from' kewyword\n";
33  return;
34  }
35 
36  std::vector< int64_t > sources;
37  unsigned int i_ptr = 2;
38 
39  for ( ; i_ptr < tokens.size(); ++i_ptr) {
40  if (tokens[i_ptr].compare("to") == 0) break;
41  try {
42  uint64_t start_vertex(stol(tokens[i_ptr], &sz));
43  sources.push_back(start_vertex);
44  } catch(...) {
45  break;
46  }
47  }
48 
49  if (i_ptr == tokens.size() || tokens[i_ptr].compare("to") != 0) {
50  std::cout << "dijkstra: 'dist' kewyword not found\n";
51  return;
52  }
53 
54  if (sources.size() == 0) {
55  std::cout << "dijkstra: No start value found\n";
56  return;
57  }
58 
59  ++i_ptr;
60  if (i_ptr == tokens.size()) {
61  std::cout << "dijkstra: No 'to' values found\n";
62  return;
63  }
64 
65  std::vector< int64_t > targets;
66  for ( ; i_ptr < tokens.size(); ++i_ptr) {
67  auto end_vertex(stol(tokens[i_ptr], &sz));
68  targets.push_back(end_vertex);
69  }
70 
71 
72  if (sources.size() == 1 && targets.size() == 1) {
73  // one to one
74  Path path;
75  pgr_dijkstra(graph, path, sources[0], targets[0]);
76  std::cout << "THE OPUTPUT ----> total cost: " << path.cost << "\n";
77  path.print_path();
78  path.clear();
79 
80 
81  } else if (sources.size() == 1 && targets.size() > 1) {
82  // one to many
83  pgr_dijkstra(graph, paths, sources[0], targets);
84 
85  std::cout << "THE OPUTPUTS ----> total outputs: " << paths.size() << "\n";
86  for (unsigned int i = 0; i < paths.size(); ++i) {
87  if (sizeof(paths[i]) == 0) continue; // no solution found
88  std::cout << "Path #" << i << " cost: " << paths[i].cost << "\n";
89  paths[i].print_path();
90  }
91  } else if (sources.size() > 1 && targets.size() == 1) {
92  // many to 1
93  std::deque<Path> paths;
94  pgr_dijkstra(graph, paths, sources, targets[0]);
95 
96 
97  std::cout << "THE OPUTPUTS ----> total outputs: " << paths.size() << "\n";
98  for (unsigned int i = 0; i < paths.size(); ++i) {
99  if (sizeof(paths[i]) == 0) continue; // no solution found
100  std::cout << "Path #" << i << " cost: " << paths[i].cost << "\n";
101  paths[i].print_path();
102  }
103  } else {
104  // many to many
105  std::deque<Path> paths;
106  pgr_dijkstra(graph, paths, sources, targets);
107 
108  std::cout << "THE OPUTPUTS ----> total outputs: " << paths.size() << "\n";
109  for (unsigned int i = 0; i < paths.size(); ++i) {
110  if (sizeof(paths[i]) == 0) continue; // no solution found
111  std::cout << "Path #" << i << " cost: " << paths[i].cost << "\n";
112  paths[i].print_path();
113  }
114  }
115 }
void process_dijkstra(G &graph, const std::vector< std::string > &tokens)
Definition: dijkstra.hpp:29
std::deque< Path > pgr_dijkstra(G &graph, std::vector< int64_t > sources, std::vector< int64_t > targets, bool only_cost, bool normal)
path_element_t * path
Definition: BDATester.cpp:49
void clear()