Parasol Planning Library (PPL)
Neighbors.h
Go to the documentation of this file.
1 #ifndef PMPL_NEIGHBORS_H_
2 #define PMPL_NEIGHBORS_H_
3 
4 #include <cstddef>
5 #include <functional>
6 #include <limits>
7 
8 
13 struct Neighbor final {
14 
17 
18  typedef size_t VID;
19 
22  typedef std::function<bool(const Neighbor& _a, const Neighbor& _b)> Comparator;
23 
27 
28  VID source{VID(-1)};
29  VID target{VID(-1)};
30 
32  double distance{std::numeric_limits<double>::infinity()};
33 
37 
39 
40  Neighbor(const VID _target, const double _distance);
41 
42  Neighbor(const VID _source, const VID _target, const double _distance);
43 
47 
49  bool operator==(const Neighbor& _other) const noexcept;
50 
52  bool operator!=(const Neighbor& _other) const noexcept;
53 
60 
62  bool operator<(const Neighbor& _other) const noexcept;
63 
65  static Comparator OrderByDistanceThenTarget() noexcept;
66 
68  static Comparator OrderByDistanceThenSource() noexcept;
69 
71  static Comparator OrderByAll() noexcept;
72 
74 
75 };
76 
77 /*------------------------------- Construction -------------------------------*/
78 
79 inline
80 Neighbor::
81 Neighbor() = default;
82 
83 
84 inline
86 Neighbor(const VID _target, const double _distance)
87  : target(_target), distance(_distance)
88 { }
89 
90 
91 inline
93 Neighbor(const VID _source, const VID _target, const double _distance)
94  : source(_source), target(_target), distance(_distance)
95 { }
96 
97 /*--------------------------------- Equality ---------------------------------*/
98 
99 inline
100 bool
102 operator==(const Neighbor& _other) const noexcept {
103  return source == _other.source
104  and target == _other.target
105  and distance == _other.distance;
106 }
107 
108 
109 inline
110 bool
112 operator!=(const Neighbor& _other) const noexcept {
113  return !(*this == _other);
114 }
115 
116 
117 /*--------------------------------- Ordering ---------------------------------*/
118 
119 inline
120 bool
122 operator<(const Neighbor& _other) const noexcept {
123  return distance < _other.distance;
124 }
125 
126 
127 inline
130 OrderByDistanceThenTarget() noexcept {
131  return [](const Neighbor& _a, const Neighbor& _b) -> bool {
132  if(_a.distance < _b.distance)
133  return true;
134  else if(_a.distance > _b.distance)
135  return false;
136  else if(_a.target < _b.target)
137  return true;
138  else if(_a.target > _b.target)
139  return false;
140  return false;
141  };
142 }
143 
144 
145 inline
148 OrderByDistanceThenSource() noexcept {
149  return [](const Neighbor& _a, const Neighbor& _b) -> bool {
150  if(_a.distance < _b.distance)
151  return true;
152  else if(_a.distance > _b.distance)
153  return false;
154  else if(_a.source < _b.source)
155  return true;
156  else if(_a.source > _b.source)
157  return false;
158  return false;
159  };
160 }
161 
162 
163 inline
166 OrderByAll() noexcept {
167  return [](const Neighbor& _a, const Neighbor& _b) -> bool {
168  if(_a.distance < _b.distance)
169  return true;
170  else if(_a.distance > _b.distance)
171  return false;
172  else if(_a.source < _b.source)
173  return true;
174  else if(_a.source > _b.source)
175  return false;
176  else if(_a.target < _b.target)
177  return true;
178  else if(_a.target > _b.target)
179  return false;
180  return false;
181  };
182 }
183 
184 /*----------------------------------------------------------------------------*/
185 
186 #endif
Definition: Neighbors.h:13
static Comparator OrderByDistanceThenSource() noexcept
Orders by distance, then source VID.
Definition: Neighbors.h:148
std::function< bool(const Neighbor &_a, const Neighbor &_b)> Comparator
Definition: Neighbors.h:22
static Comparator OrderByAll() noexcept
Orders by distance, then source, then target.
Definition: Neighbors.h:166
bool operator==(const Neighbor &_other) const noexcept
Checks equality with another Neighbor.
Definition: Neighbors.h:102
static Comparator OrderByDistanceThenTarget() noexcept
Orders by distance, then target VID.
Definition: Neighbors.h:130
double distance
The distance between source and target.
Definition: Neighbors.h:32
bool operator<(const Neighbor &_other) const noexcept
Orders by distance only.
Definition: Neighbors.h:122
bool operator!=(const Neighbor &_other) const noexcept
Checks inequality with another Neighbor.
Definition: Neighbors.h:112
size_t VID
Definition: Neighbors.h:18
VID source
The source VID.
Definition: Neighbors.h:28
VID target
The target VID.
Definition: Neighbors.h:29