PGROUTING  3.2
tour.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 
3 FILE:
4 
5 Copyright (c) 2015 pgRouting developers
7 
8 ------
9 
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14 
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 
24  ********************************************************************PGR-GNU*/
25 
26 #include "tsp/tour.h"
27 
28 #include <algorithm>
29 
30 #include "cpp_common/pgr_assert.h"
31 
32 
33 namespace pgrouting {
34 namespace tsp {
35 
36 std::ostream& operator<<(
37  std::ostream &log,
38  const Tour &tour) {
39  for (const auto &city : tour.cities) {
40  log << city << ", ";
41  }
42  return log;
43 }
44 
45 
46 
48  size_t c1,
49  size_t c2) {
50  pgassert(c1 < c2 && c2 < cities.size());
51  std::reverse(
52  cities.begin() + static_cast<difference_type>(c1 + 1),
53  cities.begin() + static_cast<difference_type>(c2 + 1));
54 }
55 
57  size_t place,
58  size_t first,
59  size_t last) {
60  pgassert(first < cities.size());
61  pgassert(last < cities.size());
62  pgassert(place < cities.size());
63 
64  if (place < first) {
65  std::rotate(
66  cities.begin() + static_cast<difference_type>(place + 1),
67  cities.begin() + static_cast<difference_type>(first + 1),
68  cities.begin() + static_cast<difference_type>(last + 1));
69  } else {
70  std::rotate(
71  cities.begin() + static_cast<difference_type>(first + 1),
72  cities.begin() + static_cast<difference_type>(last + 1),
73  cities.begin() + static_cast<difference_type>(place + 1));
74  }
75 }
76 
78  size_t c1,
79  size_t c2,
80  size_t c3) {
81  pgassert(c1 < c2 && c2 < c3 && c3 < cities.size());
82 
83  std::rotate(
84  cities.begin() + static_cast<difference_type>(c1 + 1),
85  cities.begin() + static_cast<difference_type>(c2 + 1),
86  cities.begin() + static_cast<difference_type>(c3 + 1));
87 }
88 
89 
91  size_t c1,
92  size_t c2 ) {
93  pgassert(c1 < c2);
94 
95  std::iter_swap(
96  cities.begin() +
97  static_cast<difference_type>(c1), cities.begin() +
98  static_cast<difference_type>(c2));
99 }
100 
101 
102 } // namespace tsp
103 } // namespace pgrouting
pgrouting::tsp::Tour
Definition: tour.h:42
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
pgrouting::tsp::operator<<
std::ostream & operator<<(std::ostream &log, const Dmatrix &matrix)
Definition: Dmatrix.cpp:216
pgrouting::tsp::Tour::difference_type
std::vector< size_t >::difference_type difference_type
Definition: tour.h:43
pgrouting::tsp::Tour::cities
std::vector< size_t > cities
Definition: tour.h:156
pgr_assert.h
An assert functionality that uses C++ throw().
pgrouting::tsp::Tour::rotate
void rotate(size_t c1, size_t c2, size_t c3)
Definition: tour.cpp:77
pgrouting::tsp::Tour::swap
void swap(size_t c1, size_t c2)
Definition: tour.cpp:90
pgrouting::tsp::Tour::slide
void slide(size_t place, size_t first, size_t last)
Definition: tour.cpp:56
tour.h
pgrouting
Book keeping class for swapping orders between vehicles.
Definition: pgr_alphaShape.cpp:56
pgrouting::tsp::Tour::reverse
void reverse(size_t c1, size_t c2)
Definition: tour.cpp:47