pgRouting
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
identifiers.hpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 
3 File: identifiers.hpp
4 
5 Generated with Template by:
6 Copyright (c) 2015 pgRouting developers
7 Mail: project@pgrouting.org
8 
9 Function's developer:
10 Copyright (c) 2016 Rohith Reddy
11 Mail:
12 
13 
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18 
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23 
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 
28  ********************************************************************PGR-GNU*/
29 
30 
31 #pragma once
32 #include <set>
33 #include <algorithm>
34 #include <sstream>
35 #include <iterator>
36 #include <iostream>
37 #include <stdexcept>
38 
39 template <typename T>
40 class Identifiers {
41  public:
42  Identifiers<T>() = default;
43  Identifiers<T>(T* container, size_t size) {
44  for (size_t i = 0; i < size; ++i) {
45  m_ids.insert(container[i]);
46  }
47  }
48  const std::set<T>& ids() const;
49  size_t size() const { return m_ids.size(); }
50  typedef typename std::set<T>::iterator iterator;
51  typedef typename std::set<T>::const_iterator const_iterator;
53  inline bool empty() const { return m_ids.empty(); }
54  inline void clear() { m_ids.clear(); }
55  bool has(const T other) const;
56  bool isDisjoint(const T other) const;
57  bool isDisjoint(const Identifiers<T> &other) const;
58  void insert(const Identifiers<T> &other);
59  void insert(const T &other);
60  iterator begin() { return m_ids.begin(); }
61  const_iterator begin() const { return m_ids.begin(); }
62  iterator end() { return m_ids.end(); }
63  const_iterator end() const { return m_ids.end(); }
64  bool operator ==(const Identifiers<T> &other) const;
65  const T& operator[](size_t index) const;
66  Identifiers<T> operator +(const T &other) const;
67  Identifiers<T> operator *(const T &other) const;
68  Identifiers<T> operator -(const T &other) const;
69  Identifiers<T> operator +(const Identifiers<T> &other) const;
70  Identifiers<T> operator *(const Identifiers<T> &other) const;
71  Identifiers<T> operator -(const Identifiers<T> &other) const;
72  Identifiers<T>& operator +=(const T &other);
73  Identifiers<T>& operator *=(const T &other);
74  Identifiers<T>& operator -=(const T &other);
78  template<T>
79  friend std::ostream& operator<<(
80  std::ostream& os,
81  const Identifiers<T>& identifiers);
82 
83  private:
84  std::set<T> m_ids;
85 };
86 
87 
88 
89 
91 template <typename T>
92 const std::set<T>& Identifiers<T>::ids() const {
93  return m_ids;
94 }
95 
97 
103 template <typename T>
104 bool Identifiers<T>::has(const T other) const {
105  return (m_ids.find(other) != m_ids.end());
106 }
107 
109 
115 template <typename T>
116 bool Identifiers<T>::isDisjoint(const T other) const {
117  return (m_ids.find(other) == m_ids.end());
118 }
119 
121 
127 template <typename T>
128 bool Identifiers<T>::isDisjoint(const Identifiers<T> &other) const {
129  for (auto identifier : other.ids()) {
130  if (!(m_ids.find(identifier) == m_ids.end())) {
131  return false;
132  }
133  }
134  return true;
135 }
136 
138 
141 template <typename T>
143  m_ids.insert(other.ids().begin(), other.ids().end());
144 }
145 
147 
150 template <typename T>
151 void Identifiers<T>::insert(const T &other) {
152  m_ids.insert(other);
153 }
154 
156 
162 template <typename T>
163 bool Identifiers<T>::operator ==(const Identifiers<T> &other) const {
164  return std::equal(m_ids.begin(), m_ids.end(), other.ids().begin());
165 }
166 
167 
169 
173 template <typename T>
175  Identifiers<T> union_ids;
176  union_ids.insert(*this);
177  union_ids.insert(other);
178  return union_ids;
179 }
180 
182 
186 template <typename T>
188  Identifiers<T> intersect_ids;
189  intersect_ids.insert(*this);
190  intersect_ids *= other;
191  return intersect_ids;
192 }
193 
195 
199 template <typename T>
201  Identifiers<T> diff_ids;
202  diff_ids.insert(*this);
203  diff_ids -= other;
204  return diff_ids;
205 }
206 
208 
212 template <typename T>
214  Identifiers<T> union_ids;
215  union_ids.insert(*this);
216  union_ids.insert(other);
217  return union_ids;
218 }
219 
221 
225 template <typename T>
227  Identifiers<T> intersect_ids;
228  intersect_ids.insert(*this);
229  intersect_ids *= other;
230  return intersect_ids;
231 }
232 
234 
238 template <typename T>
240  Identifiers<T> diff_ids;
241  diff_ids.insert(*this);
242  diff_ids -= other;
243  return diff_ids;
244 }
245 
246 
248 
252 template <typename T>
254  this->insert(other);
255  return *this;
256 }
257 
259 
263 template <typename T>
265  if (m_ids.find(other) != m_ids.end()) {
266  m_ids.clear();
267  m_ids.insert(other);
268  } else {
269  m_ids.clear();
270  }
271  return *this;
272 }
273 
275 
279 template <typename T>
281  if (m_ids.find(other) != m_ids.end()) {
282  m_ids.erase(m_ids.find(other));
283  }
284  return *this;
285 }
286 
287 
289 
293 template <typename T>
295  this->insert(other);
296  return *this;
297 }
298 
300 
304 template <typename T>
306  for (auto identifier : m_ids) {
307  if (other.ids().find(identifier) == other.ids().end()) {
308  m_ids.erase(m_ids.find(identifier));
309  }
310  }
311  return *this;
312 }
313 
315 
319 template <typename T>
321  for (auto identifier : m_ids) {
322  if (other.ids().find(identifier) != other.ids().end()) {
323  m_ids.erase(m_ids.find(identifier));
324  }
325  }
326  return *this;
327 }
328 
330 template <typename T>
331 std::ostream& operator<<(std::ostream& os, const Identifiers<T>& identifiers) {
332  os << "{";
333  for (auto identifier : identifiers.ids()) {
334  os << identifier << ", ";
335  }
336  os << "}";
337  return os;
338 }
339 
341 
344 template <typename T>
345 const T& Identifiers<T>::operator[](size_t index) const {
346  if (index >= size()) {
347  throw std::out_of_range("Index out of bounds");
348  }
349  return *std::next(m_ids.begin(), index);
350 }
bool has(const T other) const
Returns a boolean value true or false.
Identifiers< T > & operator+=(const T &other)
Returns a set of identifiers of type Identifiers<T>&
std::set< T > m_ids
Definition: identifiers.hpp:84
const_iterator begin() const
Definition: identifiers.hpp:61
const_iterator end() const
Definition: identifiers.hpp:63
const std::set< T > & ids() const
Returns a set of identifiers of type set<T>
Definition: identifiers.hpp:92
bool isDisjoint(const T other) const
Returns a boolean value true or false.
Identifiers< T > & operator*=(const T &other)
Returns a set of identifiers of type Identifiers<T>&
void clear()
Definition: identifiers.hpp:54
bool operator==(const Identifiers< T > &other) const
Returns a boolean value.
const T & operator[](size_t index) const
Returns an identifier of type T at position index in the set.
iterator begin()
Definition: identifiers.hpp:60
std::set< T >::iterator iterator
Definition: identifiers.hpp:50
void insert(const Identifiers< T > &other)
Inserts a set of identifiers of type Identifiers<T> to this set.
friend std::ostream & operator<<(std::ostream &os, const Identifiers< T > &identifiers)
Prints the set of identifiers.
Identifiers< T > operator*(const T &other) const
Returns a set of identifiers of type Identifiers<T>
size_t size() const
Definition: identifiers.hpp:49
Identifiers< T > & operator-=(const T &other)
Returns a set of identifiers of type Identifiers<T>&
bool empty() const
Returns true when the set is empty.
Definition: identifiers.hpp:53
Identifiers< T > operator+(const T &other) const
Returns a set of identifiers of type Identifiers<T>
iterator end()
Definition: identifiers.hpp:62
Identifiers< T > operator-(const T &other) const
Returns a set of identifiers of type Identifiers<T>
std::set< T >::const_iterator const_iterator
Definition: identifiers.hpp:51