PGROUTING  2.4
 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 #ifndef SRC_COMMON_SRC_IDENTIFIERS_HPP_
31 #define SRC_COMMON_SRC_IDENTIFIERS_HPP_
32 #pragma once
33 
34 #include <set>
35 #include <algorithm>
36 #include <sstream>
37 #include <iterator>
38 #include <iostream>
39 #include <stdexcept>
40 
41 template <typename T>
42 class Identifiers {
43  public:
44  typedef typename std::set<T>::iterator iterator;
45  typedef typename std::set<T>::const_iterator const_iterator;
46 
47  Identifiers<T>() = default;
48  /* TODO avoid pointers */
49  Identifiers<T>(T* container, size_t size) {
50  for (size_t i = 0; i < size; ++i) {
51  m_ids.insert(container[i]);
52  }
53  }
54 
55  const std::set<T>& ids() const;
56  size_t size() const { return m_ids.size(); }
58  inline bool empty() const { return m_ids.empty(); }
59  inline void clear() { m_ids.clear(); }
60  bool has(const T element) const;
61  bool isDisjoint(const T other) const;
62  bool isDisjoint(const Identifiers<T> &other) const;
63  void insert(const Identifiers<T> &other);
64  void insert(const T &other);
65  iterator begin() { return m_ids.begin(); }
66  const_iterator begin() const { return m_ids.begin(); }
67  iterator end() { return m_ids.end(); }
68  const_iterator end() const { return m_ids.end(); }
69  bool operator ==(const Identifiers<T> &other) const;
70  const T& operator[](size_t index) const;
71 
74 
75  Identifiers<T> operator +(const T &other) const;
76  Identifiers<T> operator *(const T &other) const;
77  Identifiers<T> operator -(const T &other) const;
78  Identifiers<T> operator +(const Identifiers<T> &other) const;
79  Identifiers<T> operator *(const Identifiers<T> &other) const;
80  Identifiers<T> operator -(const Identifiers<T> &other) const;
81  Identifiers<T>& operator +=(const T &other);
82  Identifiers<T>& operator *=(const T &other);
83  Identifiers<T>& operator -=(const T &other);
88 
89  template<T>
90  friend std::ostream& operator<<(
91  std::ostream& os,
92  const Identifiers<T>& identifiers);
93 
94  private:
95  std::set<T> m_ids;
96 };
97 
98 
99 
100 
102 template <typename T>
103 const std::set<T>& Identifiers<T>::ids() const {
104  return m_ids;
105 }
106 
108 
114 template <typename T>
115 bool Identifiers<T>::has(const T other) const {
116  return (m_ids.find(other) != m_ids.end());
117 }
118 
120 
126 template <typename T>
127 bool Identifiers<T>::isDisjoint(const T other) const {
128  return (m_ids.find(other) == m_ids.end());
129 }
130 
132 
138 template <typename T>
139 bool Identifiers<T>::isDisjoint(const Identifiers<T> &other) const {
140  for (auto identifier : other.ids()) {
141  if (!(m_ids.find(identifier) == m_ids.end())) {
142  return false;
143  }
144  }
145  return true;
146 }
147 
149 
152 template <typename T>
154  m_ids.insert(other.ids().begin(), other.ids().end());
155 }
156 
158 
161 template <typename T>
162 void Identifiers<T>::insert(const T &other) {
163  m_ids.insert(other);
164 }
165 
167 
173 template <typename T>
174 bool Identifiers<T>::operator ==(const Identifiers<T> &other) const {
175  return std::equal(m_ids.begin(), m_ids.end(), other.ids().begin());
176 }
177 
178 
180 
184 template <typename T>
186  Identifiers<T> union_ids;
187  union_ids.insert(*this);
188  union_ids.insert(other);
189  return union_ids;
190 }
191 
193 
197 template <typename T>
199  Identifiers<T> intersect_ids;
200  intersect_ids.insert(*this);
201  intersect_ids *= other;
202  return intersect_ids;
203 }
204 
206 
210 template <typename T>
212  Identifiers<T> diff_ids;
213  diff_ids.insert(*this);
214  diff_ids -= other;
215  return diff_ids;
216 }
217 
219 
223 template <typename T>
225  Identifiers<T> union_ids;
226  union_ids.insert(*this);
227  union_ids.insert(other);
228  return union_ids;
229 }
230 
232 
236 template <typename T>
238  Identifiers<T> intersect_ids;
239  intersect_ids.insert(*this);
240  intersect_ids *= other;
241  return intersect_ids;
242 }
243 
245 
249 template <typename T>
251  Identifiers<T> diff_ids;
252  diff_ids.insert(*this);
253  diff_ids -= other;
254  return diff_ids;
255 }
256 
257 
259 
263 template <typename T>
265  this->insert(other);
266  return *this;
267 }
268 
270 
274 template <typename T>
276  if (m_ids.find(other) != m_ids.end()) {
277  m_ids.clear();
278  m_ids.insert(other);
279  } else {
280  m_ids.clear();
281  }
282  return *this;
283 }
284 
286 
290 template <typename T>
292  if (m_ids.find(other) != m_ids.end()) {
293  m_ids.erase(m_ids.find(other));
294  }
295  return *this;
296 }
297 
298 
300 
304 template <typename T>
306  this->insert(other);
307  return *this;
308 }
309 
311 
315 template <typename T>
317  for (auto identifier : m_ids) {
318  if (other.ids().find(identifier) == other.ids().end()) {
319  m_ids.erase(m_ids.find(identifier));
320  }
321  }
322  return *this;
323 }
324 
326 
330 template <typename T>
332  for (auto identifier : m_ids) {
333  if (other.ids().find(identifier) != other.ids().end()) {
334  m_ids.erase(m_ids.find(identifier));
335  }
336  }
337  return *this;
338 }
339 
341 template <typename T>
342 std::ostream& operator<<(std::ostream& os, const Identifiers<T>& identifiers) {
343  os << "{";
344  for (auto identifier : identifiers.ids()) {
345  os << identifier << ", ";
346  }
347  os << "}";
348  return os;
349 }
350 
352 
355 template <typename T>
356 const T& Identifiers<T>::operator[](size_t index) const {
357  if (index >= size()) {
358  throw std::out_of_range("Index out of bounds");
359  }
360  return *std::next(m_ids.begin(), index);
361 }
362 
363 
364 #endif // SRC_COMMON_SRC_IDENTIFIERS_HPP_
Identifiers< T > & operator+=(const T &other)
Returns a set of identifiers of type Identifiers<T>&
bool has(const T element) const
Returns a boolean value true or false.
std::set< T > m_ids
Definition: identifiers.hpp:95
const_iterator begin() const
Definition: identifiers.hpp:66
const_iterator end() const
Definition: identifiers.hpp:68
const std::set< T > & ids() const
Returns a set of identifiers of type set<T>
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:59
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:65
std::set< T >::iterator iterator
Definition: identifiers.hpp:44
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:56
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:58
Identifiers< T > operator+(const T &other) const
Returns a set of identifiers of type Identifiers<T>
iterator end()
Definition: identifiers.hpp:67
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:45