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