PGROUTING  3.2
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
8 
9 Function's developer:
10 Copyright (c) 2016 Rohith Reddy
11 Mail:
12 
13 ------
14 
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
19 
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24 
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 
29  ********************************************************************PGR-GNU*/
30 
33 #ifndef INCLUDE_CPP_COMMON_IDENTIFIERS_HPP_
34 #define INCLUDE_CPP_COMMON_IDENTIFIERS_HPP_
35 #pragma once
36 
37 #include <set>
38 #include <algorithm>
39 #include <iterator>
40 #include <sstream>
41 #include <iostream>
42 #include <stdexcept>
43 
44 /* TODO(vicky)
45  * compiler check that type T is a integral type
46  */
47 
48 template <typename T>
49 class Identifiers {
50  public:
51  typedef typename std::set<T>::iterator iterator;
52  typedef typename std::set<T>::const_iterator const_iterator;
53 
54 
56 
57  Identifiers<T>() = default;
58  Identifiers<T>(const Identifiers<T>&) = default;
59  Identifiers<T>(const std::set<T>& data) {
60  m_ids = data;
61  }
62 
63  /* @brief initializes with {1 ~ number}
64  *
65  * @params [in] number
66  */
67  explicit Identifiers<T>(const size_t number) {
68  size_t i(0);
69  std::generate_n(std::inserter(m_ids, m_ids.begin()),
70  number,
71  [&i](){ return i++; });
72  }
73 
75 
77 
78  size_t size() const {return m_ids.size(); }
79  inline bool empty() const {return m_ids.empty(); }
80  inline T front() const {return *m_ids.begin();}
81  const_iterator begin() const {return m_ids.begin();}
82  const_iterator end() const {return m_ids.end();}
83  inline void pop_front() {m_ids.erase(m_ids.begin());}
84  inline void clear() {m_ids.clear();}
85  iterator begin() {return m_ids.begin();}
86  iterator end() {return m_ids.end();}
88 
89 
90  private:
91  std::set<T> m_ids;
92 
93  public:
95 
98  bool has(const T other) const {
99  return (m_ids.find(other) != m_ids.end());
100  }
101 
102 
104 
107  bool operator==(const Identifiers<T> &rhs) const {
108  return std::equal(m_ids.begin(), m_ids.end(), rhs.m_ids.begin());
109  }
110 
113 
120  const Identifiers<T> &lhs,
121  const Identifiers<T> &rhs) {
122  Identifiers<T> union_ids(lhs);
123  union_ids += rhs;
124  return union_ids;
125  }
126 
128 
132  const Identifiers<T> &other) {
133  m_ids.insert(other.m_ids.begin(), other.m_ids.end());
134  return *this;
135  }
137 
140  Identifiers<T>& operator +=(const T &element) {
141  m_ids.insert(element);
142  return *this;
143  }
144 
146 
147 
148 
151 
160  const Identifiers<T> &lhs,
161  const Identifiers<T> &rhs) {
162  std::set<T> result;
163  std::set_intersection(
164  lhs.m_ids.begin(), lhs.m_ids.end(),
165  rhs.m_ids.begin(), rhs.m_ids.end(),
166  std::inserter(result, result.begin()));
167  return Identifiers<T>(result);
168  }
169 
171 
175  const Identifiers<T> &other) {
176  *this = *this * other;
177  return *this;
178  }
179 
181 
184  Identifiers<T>& operator *=(const T &element) {
185  if (has(element)) {
186  m_ids.clear();
187  m_ids.insert(element);
188  } else {
189  m_ids.clear();
190  }
191  return *this;
192  }
193 
195 
196 
199 
200  /* \brief set DIFFERENCE set
201  *
202  * @param[in] lhs Identifiers
203  * @param[in] rhs Identifiers
204  */
205  friend
207  const Identifiers<T> &lhs,
208  const Identifiers<T> &rhs) {
209  std::set<T> result;
210  std::set_difference(
211  lhs.m_ids.begin(), lhs.m_ids.end(),
212  rhs.m_ids.begin(), rhs.m_ids.end(),
213  std::inserter(result, result.begin()));
214  return Identifiers<T>(result);
215  }
216 
217 
218 
220 
225  *this = *this - other;
226  return *this;
227  }
228 
230 
233  Identifiers<T>& operator -=(const T &element) {
234  m_ids.erase(element);
235  return *this;
236  }
237 
239 
241  friend
242  std::ostream&
243  operator<<(std::ostream& os, const Identifiers<T>& identifiers) {
244  os << "{";
245  for (auto identifier : identifiers.m_ids) {
246  os << identifier << ", ";
247  }
248  os << "}";
249  return os;
250  }
251 
253 };
254 
255 #endif // INCLUDE_CPP_COMMON_IDENTIFIERS_HPP_
Identifiers::operator*=
Identifiers< T > & operator*=(const Identifiers< T > &other)
coumpound set INTERSECTION set
Definition: identifiers.hpp:174
Identifiers::pop_front
void pop_front()
Definition: identifiers.hpp:83
Identifiers::operator-
friend Identifiers< T > operator-(const Identifiers< T > &lhs, const Identifiers< T > &rhs)
Definition: identifiers.hpp:206
Identifiers::m_ids
std::set< T > m_ids
Definition: identifiers.hpp:91
Identifiers::begin
iterator begin()
Definition: identifiers.hpp:85
Identifiers::size
size_t size() const
Definition: identifiers.hpp:78
Identifiers::clear
void clear()
Definition: identifiers.hpp:84
Identifiers::operator+
friend Identifiers< T > operator+(const Identifiers< T > &lhs, const Identifiers< T > &rhs)
set UNION set
Definition: identifiers.hpp:119
Identifiers::operator*
friend Identifiers< T > operator*(const Identifiers< T > &lhs, const Identifiers< T > &rhs)
set INTERSECTION
Definition: identifiers.hpp:159
Identifiers::begin
const_iterator begin() const
Definition: identifiers.hpp:81
Identifiers::empty
bool empty() const
Definition: identifiers.hpp:79
Identifiers::operator+=
Identifiers< T > & operator+=(const Identifiers< T > &other)
compound set UNION set
Definition: identifiers.hpp:131
Identifiers::iterator
std::set< T >::iterator iterator
Definition: identifiers.hpp:51
Identifiers::front
T front() const
Definition: identifiers.hpp:80
Identifiers::end
const_iterator end() const
Definition: identifiers.hpp:82
Identifiers::has
bool has(const T other) const
true ids() has element
Definition: identifiers.hpp:98
Identifiers::operator<<
friend std::ostream & operator<<(std::ostream &os, const Identifiers< T > &identifiers)
Prints the set of identifiers.
Definition: identifiers.hpp:243
Identifiers::operator==
bool operator==(const Identifiers< T > &rhs) const
true when both sets are equal
Definition: identifiers.hpp:107
Identifiers::end
iterator end()
Definition: identifiers.hpp:86
Identifiers::operator-=
Identifiers< T > & operator-=(const Identifiers< T > &other)
compound set DIFFERENCE set
Definition: identifiers.hpp:224
Identifiers::const_iterator
std::set< T >::const_iterator const_iterator
Definition: identifiers.hpp:52
Identifiers
Definition: identifiers.hpp:49