PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
basePath_SSEC.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: basePath_SSEC.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 
22 ********************************************************************PGR-GNU*/
23 
24 #include "./basePath_SSEC.hpp"
25 
26 #include <deque>
27 #include <iostream>
28 #include <algorithm>
29 #include <utility>
30 
31 #include "./pgr_types.h"
32 #include "./pgr_assert.h"
33 
35  path.push_front(data);
36  m_tot_cost += data.cost;
37 }
38 
39 void Path::push_back(Path_t data) {
40  path.push_back(data);
41  m_tot_cost += data.cost;
42 }
43 
44 void Path::reverse() {
45  std::swap(m_start_id, m_end_id);
46  if (path.size() <= 1) return;
47  std::deque< Path_t > newpath;
48  for (size_t i = 0; i < path.size(); ++i) {
49  newpath.push_front({
50  path[i].node,
51  (i == 0? -1 : path[i - 1].edge),
52  (i == 0? 0 : path[i - 1].cost),
53  0
54  });
55  }
56  for (size_t i = 0; i < newpath.size(); ++i) {
57  newpath[i].agg_cost = (i == 0)?
58  0 :
59  newpath[i - 1].agg_cost + newpath[i - 1].cost;
60  }
61  path = newpath;
62 }
63 
64 
65 void Path::clear() {
66  path.clear();
67  m_tot_cost = 0;
68  m_start_id = 0;
69  m_end_id = 0;
70 }
71 
72 std::ostream& operator<<(std::ostream &log, const Path &path) {
73  log << "Path: " << path.start_id() << " -> " << path.end_id() << "\n"
74  << "seq\tnode\tedge\tcost\tagg_cost\n";
75  int64_t i = 0;
76  for (const auto &e : path) {
77  log << i << "\t"
78  << e.node << "\t"
79  << e.edge << "\t"
80  << e.cost << "\t"
81  << e.agg_cost << "\n";
82  ++i;
83  }
84  return log;
85 }
86 
87 
88 
89 Path Path::getSubpath(unsigned int j) const {
90  Path result(start_id(), end_id());
91  if (j == 0) return result;
92  for (auto i = path.begin(); i != path.begin() + j; ++i) {
93  result.push_back((*i));
94  }
95  pgassert(result.tot_cost() != 0);
96  pgassert(this->tot_cost() != 0);
97  return result;
98 }
99 
100 
101 bool Path::isEqual(const Path &subpath) const {
102  if (subpath.empty()) return true;
103  if (subpath.size() >= path.size()) return false;
104  std::deque< Path_t >::const_iterator i, j;
105  for (i = path.begin(), j = subpath.begin();
106  j != subpath.end();
107  ++i, ++j)
108  if ((*i).node != (*j).node) return false;
109  return true;
110 }
111 
112 void Path::appendPath(const Path &o_path) {
113  path.insert(path.end(), o_path.path.begin(), o_path.path.end());
114  m_tot_cost += o_path.m_tot_cost;
115 }
116 
117 
141 void Path::append(const Path &other) {
142  pgassert(m_end_id == other.m_start_id);
143  if (other.m_start_id == other.m_end_id) {
144  pgassert(other.path.empty());
145  return;
146  }
147  if (m_start_id == m_end_id) {
148  pgassert(path.empty());
149  *this = other;
150  return;
151  }
152  pgassert(path.back().cost == 0);
153  pgassert(path.back().edge == -1);
154 
155  m_end_id = other.m_end_id;
156 
157  auto last = path.back();
158  auto agg_cost = last.agg_cost;
159 
160  path.pop_back();
161 
162  for (auto item : other.path) {
163  item.agg_cost += agg_cost;
164  push_back(item);
165  }
166 }
167 
168 
170  General_path_element_t **postgres_data,
171  size_t &sequence) const {
172  int i = 1;
173  for (const auto e : path) {
174  (*postgres_data)[sequence] =
175  {i, start_id(), end_id(), e.node, e.edge, e.cost, e.agg_cost};
176  ++i;
177  ++sequence;
178  }
179 }
180 
181 /* used by driving distance */
183  General_path_element_t **ret_path,
184  size_t &sequence) const {
185  for (unsigned int i = 0; i < path.size(); i++) {
186  (*ret_path)[sequence].seq = i;
187  (*ret_path)[sequence].start_id = start_id();
188  (*ret_path)[sequence].end_id = start_id();
189  (*ret_path)[sequence].node = path[i].node;
190  (*ret_path)[sequence].edge = path[i].edge;
191  (*ret_path)[sequence].cost = path[i].cost;
192  (*ret_path)[sequence].agg_cost = path[i].agg_cost;
193  sequence++;
194  }
195 }
196 
197 /* used by ksp */
199  General_path_element_t **ret_path,
200  size_t &sequence, int routeId) const {
201  for (unsigned int i = 0; i < path.size(); i++) {
202  (*ret_path)[sequence].seq = i + 1;
203  (*ret_path)[sequence].start_id = routeId;
204  (*ret_path)[sequence].end_id = end_id();
205  (*ret_path)[sequence].node = path[i].node;
206  (*ret_path)[sequence].edge = path[i].edge;
207  (*ret_path)[sequence].cost = path[i].cost;
208  (*ret_path)[sequence].agg_cost = (i == 0)?
209  0 :
210  (*ret_path)[sequence-1].agg_cost + path[i-1].cost;
211  sequence++;
212  }
213 }
int64_t m_end_id
void push_front(Path_t data)
int64_t m_start_id
void get_pg_ksp_path(General_path_element_t **ret_path, size_t &sequence, int routeId) const
void get_pg_dd_path(General_path_element_t **ret_path, size_t &sequence) const
double cost
Definition: pgr_types.h:99
void push_back(Path_t data)
void generate_postgres_data(General_path_element_t **postgres_data, size_t &sequence) const
int64_t end_id() const
double tot_cost() const
std::ostream & operator<<(std::ostream &log, const Path &path)
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:81
pthIt end()
void reverse()
void append(const Path &other)
Path: 2 -> 9 seq node edge cost agg_cost 0 2 4 1 0 1 5 8 1 1 2 6 9 1 2 3 9 -1 0 3 Path: 9 -> 3 seq no...
int64_t start_id() const
pthIt begin()
bool empty() const
Assertions Handling.
path_element_t * path
Definition: BDATester.cpp:49
Path getSubpath(unsigned int j) const
size_t size() const
void clear()
std::deque< Path_t > path
double m_tot_cost
void appendPath(const Path &o_path)
bool isEqual(const Path &subpath) const