PGROUTING  3.2
basic_vertex.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2  *
3 
4 Copyright (c) 2015 Celia Virginia Vergara Castillo
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 
26 
27 #include <vector>
28 #include <algorithm>
29 #include <sstream>
30 #include <ostream>
31 
32 #include "cpp_common/pgr_assert.h"
33 
34 
35 namespace pgrouting {
36 
37  std::ostream& operator<<(std::ostream& log, const Basic_vertex &v) {
38  log << v.id;
39  return log;
40  }
41 
43  std::vector < Basic_vertex > vertices) {
44  auto count(vertices.size());
45  std::stable_sort(
46  vertices.begin(), vertices.end(),
47  [](const Basic_vertex &lhs, const Basic_vertex &rhs)
48  {return lhs.id < rhs.id;});
49  vertices.erase(
50  std::unique(
51  vertices.begin(), vertices.end(),
52  [](const Basic_vertex &lhs, const Basic_vertex &rhs)
53  {return lhs.id == rhs.id;}), vertices.end());
54 
55  return vertices.size() - count;
56  }
57 
58 
59  std::vector < Basic_vertex > extract_vertices(
60  std::vector < Basic_vertex > vertices,
61  const std::vector <pgr_edge_t > data_edges) {
62  if (data_edges.empty()) return vertices;
63 
64  vertices.reserve(vertices.size() + data_edges.size() * 2);
65 
66  for (const auto edge : data_edges) {
67  Basic_vertex vertex;
68 
69  vertex.id = edge.source;
70  vertices.push_back(vertex);
71 
72  vertex.id = edge.target;
73  vertices.push_back(vertex);
74  }
75 
76  /*
77  * sort and delete duplicates
78  */
79  std::stable_sort(vertices.begin(), vertices.end(),
80  [](const Basic_vertex &lhs, const Basic_vertex &rhs)
81  {return lhs.id < rhs.id;});
82 
83  vertices.erase(
84  std::unique(vertices.begin(), vertices.end(),
85  [](const Basic_vertex &lhs, const Basic_vertex &rhs)
86  {return lhs.id == rhs.id;}), vertices.end());
87 
88  return vertices;
89  }
90 
91 
92  std::vector < Basic_vertex > extract_vertices(
93  const std::vector <pgr_edge_t > &data_edges) {
94  std::vector< Basic_vertex > vertices;
95  if (data_edges.empty()) return vertices;
96 
97  vertices.reserve(data_edges.size() * 2);
98 
99  for (const auto edge : data_edges) {
100  Basic_vertex vertex;
101 
102  vertex.id = edge.source;
103  vertices.push_back(vertex);
104 
105  vertex.id = edge.target;
106  vertices.push_back(vertex);
107  }
108 
109  /*
110  * sort and delete duplicates
111  */
112  std::stable_sort(vertices.begin(), vertices.end(),
113  [](const Basic_vertex &lhs, const Basic_vertex &rhs)
114  {return lhs.id < rhs.id;});
115 
116  vertices.erase(
117  std::unique(vertices.begin(), vertices.end(),
118  [](const Basic_vertex &lhs, const Basic_vertex &rhs)
119  {return lhs.id == rhs.id;}), vertices.end());
120 
121  return vertices;
122  }
123 
124  std::vector < Basic_vertex > extract_vertices(
125  const pgr_edge_t *data_edges, size_t count) {
126  return extract_vertices(
127  std::vector < pgr_edge_t >(
128  data_edges, data_edges + count));
129  }
130 
131  std::vector < Basic_vertex > extract_vertices(
132  const std::vector < Basic_vertex > &vertices,
133  const pgr_edge_t *data_edges, size_t count) {
134  return extract_vertices(
135  vertices, std::vector < pgr_edge_t >(
136  data_edges, data_edges + count));
137  }
138 
139 } // namespace pgrouting
edge::target
int64 target
Definition: trsp.h:44
pgr_edge_t
Definition: pgr_edge_t.h:37
pgrouting::check_vertices
size_t check_vertices(std::vector< Basic_vertex > vertices)
Definition: basic_vertex.cpp:42
edge
Definition: trsp.h:41
pgrouting::operator<<
std::ostream & operator<<(std::ostream &log, const Basic_vertex &v)
Definition: basic_vertex.cpp:37
edge::source
int64 source
Definition: trsp.h:43
pgrouting::Basic_vertex
Definition: basic_vertex.h:40
pgr_assert.h
An assert functionality that uses C++ throw().
basic_vertex.h
pgrouting::extract_vertices
std::vector< Basic_vertex > extract_vertices(std::vector< Basic_vertex > vertices, const std::vector< pgr_edge_t > data_edges)
Definition: basic_vertex.cpp:59
pgrouting::Basic_vertex::id
int64_t id
Definition: basic_vertex.h:62
pgrouting
Book keeping class for swapping orders between vehicles.
Definition: pgr_alphaShape.cpp:56