OpenLibrary
std_types.hpp
1 
2 /*************************************************
3  * standard types for A* routing algorithm *
4  * Author: MichaƂ Walenciak *
5  * Creation date: 18.08.2012 *
6  *************************************************/
7 
8 
9 
10 #ifndef OPENLIBRARY_A_START_ROUTER_STD_TYPES_HPP
11 #define OPENLIBRARY_A_START_ROUTER_STD_TYPES_HPP
12 
13 #include <assert.h>
14 #include <set>
15 #include <vector>
16 #include <iostream>
17 
18 
19 namespace ol
20 {
21  namespace Router
22  {
23 
24  template<class CoordT>
25  struct Coordinates
26  {
27  CoordT x, y; //coordinates
28 
29  Coordinates(CoordT _x, CoordT _y): x(_x), y(_y) {}
30  virtual ~Coordinates() {}
31 
32  //may be used by set/map:
33  bool operator< (const Coordinates &other) const
34  {
35  bool result = false;
36 
37  //there may be some problems with floatingpoint types
38  if (y < other.y)
39  result = true;
40  else if (y > other.y)
41  result = false;
42  else
43  result = x < other.x;
44 
45  return result;
46  }
47 
48  bool operator==(const Coordinates &other) const
49  {
50  return x == other.x && y == other.y;
51  }
52 
53  friend std::ostream& operator<<(std::ostream &stream, const Coordinates &c)
54  {
55  stream << c.x << ", " << c.y;
56 
57  return stream;
58  }
59  };
60 
61  template<class CoordT, class ScoreT>
62  struct Point: Coordinates<CoordT>
63  {
64  ScoreT f_score, g_score; //scores
65  Point *origin; //origin of point
66 
67  Point(const CoordT &_x, const CoordT &_y, Point *p = nullptr): Coordinates<CoordT>(_x, _y), f_score(0), g_score(0), origin(p) {}
68 
69 //remove this macro when all supported compiler will support c++11
70 #if __cplusplus == 201103L
71  Point(Point && ) = delete;
72  Point(const Point &) = delete;
73  virtual ~Point() {}
74 
75  Point &operator=(Point && ) = delete;
76  Point &operator=(Point &) = delete;
77 #endif
78 
79  friend std::ostream& operator<<(std::ostream &stream, const Point &p)
80  {
81  stream << static_cast<const Coordinates<CoordT> &>(p);
82 
83  stream << "; q_score = " << p.g_score << ", f_score = " << p.f_score;
84 
85  return stream;
86  }
87  };
88 
89  //used for comparing points itself, not pointers to them
90  template<class PointT>
92  {
93  public:
94  bool operator() (const PointT *const p1, const PointT *const p2) const
95  {
96  return *p1 < *p2;
97  }
98  };
99 
100 
101  //basic implementation of OpenSet type for A* router.
102  //PointT must meet the same rules as required for 'PointT' in router.hpp
103  //
104  //SortedPointT is a container which implements functions:
105  //* void insert(PointT *) - for adding point
106  //* PointT* first() - for getting first (best) point. Function must remove point from container
107  //* void clear() - for clearing container. Contained pointers cannot be destroyed
108  //* size_t size() - number of elements
109  template<class PointT, class SortedPointT>
110  class OpenSet
111  {
112  public:
113  OpenSet(): m_points(), m_value() {}
114  virtual ~OpenSet()
115  {
116  clear();
117  }
118 
119  PointT *getBest()
120  {
121  PointT *result = m_value.first();
122 
123  m_points.erase(result); //TODO: optimize it
124 
125  assert(m_points.size() == m_value.size());
126 
127  return result;
128  }
129 
130  bool exists(const PointT *p, PointT* &ret) const
131  {
132  typename std::set<PointT *>::const_iterator it = m_points.find(const_cast<PointT *>(p));
133  const bool successed = it != m_points.end();
134 
135  if (successed)
136  ret = *it;
137 
138  return successed;
139  }
140 
141  void clear()
142  {
143  //free memory
144  for(auto item: m_points) //m_points and m_value keep the same points, delete them once
145  delete item;
146 
147  //remove items
148  m_points.clear();
149  m_value.clear();
150  }
151 
152  void insert(PointT *p)
153  {
154  m_points.insert(p);
155  m_value.insert(p);
156 
157  assert(m_points.size() == m_value.size());
158  }
159 
160  bool isEmpty() const
161  {
162  assert(m_points.size() == m_value.size());
163 
164  return m_points.empty();
165  }
166 
167 
168  friend std::ostream& operator<<(std::ostream &stream, const OpenSet &openSet)
169  {
170  stream << "[" << openSet.m_points.size() << "] [";
171 
172  for (auto item: openSet.m_points)
173  stream << " (" << (*item) << ")";
174 
175  stream << " ]";
176 
177  return stream;
178  }
179 
180  private:
181  std::set<PointT *, CompareCoordinates<PointT>> m_points; //for point fast finding
182  SortedPointT m_value; //for points sorting
183  };
184 
185 
186  //basic implementation of ClosedSet type for A* router.
187  template<class PointT>
188  class ClosedSet: public std::set<PointT *>
189  {
190  public:
191  ClosedSet(): std::set<PointT *>() {}
192  virtual ~ClosedSet()
193  {
194  clear();
195  }
196 
197  bool exists(const PointT *p) const
198  {
199  typename std::set<PointT *>::const_iterator it = this->find(const_cast<PointT *>(p));
200  const bool status = it != this->end();
201 
202  return status;
203  }
204 
205  void clear()
206  {
207  //free memory
208  for(auto item: *this)
209  delete item;
210 
211  //remove items
212  std::set<PointT *>::clear();
213  }
214 
215  friend std::ostream& operator<<(std::ostream &stream, const ClosedSet &closedSet)
216  {
217  stream << "[" << closedSet.size() << "] [";
218 
219  for (auto item: closedSet)
220  stream << " (" << (*item) << ")";
221 
222  stream << " ]";
223 
224  return stream;
225  }
226  };
227 
228  }
229 }
230 
231 #endif
232 
Definition: std_types.hpp:62
Definition: debug.hpp:45
Definition: std_types.hpp:110
Definition: std_types.hpp:91
Definition: std_types.hpp:188
Definition: std_types.hpp:25