vrpRouting  0.3
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 
35 #ifndef INCLUDE_CPP_COMMON_IDENTIFIERS_HPP_
36 #define INCLUDE_CPP_COMMON_IDENTIFIERS_HPP_
37 #pragma once
38 
39 #include <set>
40 #include <algorithm>
41 #include <iterator>
42 #include <sstream>
43 #include <iostream>
44 #include <stdexcept>
45 
46 /* TODO(vicky)
47  * compiler check that type T is a integral type
48  */
49 
50 template <typename T>
51 class Identifiers {
52  public:
53  typedef typename std::set<T>::iterator iterator;
54  typedef typename std::set<T>::const_iterator const_iterator;
55 
56 
58 
59  Identifiers<T>() = default;
60  Identifiers<T>(const Identifiers<T>&) = default;
61  explicit Identifiers<T>(const std::set<T>& data) {
62  m_ids = data;
63  }
64 
65  /* @brief initializes with {0 ~ number - 1}
66  *
67  * @params [in] number
68  */
69  explicit Identifiers<T>(const size_t number) {
70  std::generate_n(std::inserter(m_ids, m_ids.end()), number, [&]{ return m_ids.size(); });
71  }
72 
74 
76 
77  size_t size() const {return m_ids.size(); }
78  inline bool empty() const {return m_ids.empty(); }
79  inline T front() const {return *m_ids.begin();}
80  inline T back() const {return *m_ids.end();}
81  const_iterator begin() const {return m_ids.begin();}
82  const_iterator end() const {return m_ids.end();}
83 
84  inline void pop_front() {m_ids.erase(m_ids.begin());}
85 
86  inline void clear() {m_ids.clear();}
87  iterator begin() {return m_ids.begin();}
88  iterator end() {return m_ids.end();}
90 
91 
92  private:
93  std::set<T> m_ids;
94 
95  public:
97 
100  bool has(const T other) const {
101  return (m_ids.find(other) != m_ids.end());
102  }
103 
104 
106 
109  bool operator==(const Identifiers<T> &rhs) const {
110  return m_ids.size() == rhs.m_ids.size() && std::equal(m_ids.begin(), m_ids.end(), rhs.m_ids.begin());
111  }
112 
115 
122  const Identifiers<T> &lhs,
123  const Identifiers<T> &rhs) {
124  Identifiers<T> union_ids(lhs);
125  union_ids += rhs;
126  return union_ids;
127  }
128 
130 
134  const Identifiers<T> &other) {
135  m_ids.insert(other.m_ids.begin(), other.m_ids.end());
136  return *this;
137  }
139 
142  Identifiers<T>& operator +=(const T &element) {
143  m_ids.insert(element);
144  return *this;
145  }
146 
148 
149 
150 
153 
162  const Identifiers<T> &lhs,
163  const Identifiers<T> &rhs) {
164  std::set<T> result;
165  std::set_intersection(
166  lhs.m_ids.begin(), lhs.m_ids.end(),
167  rhs.m_ids.begin(), rhs.m_ids.end(),
168  std::inserter(result, result.begin()));
169  return Identifiers<T>(result);
170  }
171 
173 
177  const Identifiers<T> &other) {
178  *this = *this * other;
179  return *this;
180  }
181 
183 
186  Identifiers<T>& operator *=(const T &element) {
187  if (has(element)) {
188  m_ids.clear();
189  m_ids.insert(element);
190  } else {
191  m_ids.clear();
192  }
193  return *this;
194  }
195 
197 
198 
201 
202  /* \brief set DIFFERENCE set
203  *
204  * @param[in] lhs Identifiers
205  * @param[in] rhs Identifiers
206  */
207  friend
209  const Identifiers<T> &lhs,
210  const Identifiers<T> &rhs) {
211  std::set<T> result;
212  std::set_difference(
213  lhs.m_ids.begin(), lhs.m_ids.end(),
214  rhs.m_ids.begin(), rhs.m_ids.end(),
215  std::inserter(result, result.begin()));
216  return Identifiers<T>(result);
217  }
218 
219 
220 
222 
227  *this = *this - other;
228  return *this;
229  }
230 
232 
235  Identifiers<T>& operator -=(const T &element) {
236  m_ids.erase(element);
237  return *this;
238  }
239 
241 
243  friend
244  std::ostream&
245  operator<<(std::ostream& os, const Identifiers<T>& identifiers) {
246  os << "{";
247  for (auto identifier : identifiers.m_ids) {
248  os << identifier << ", ";
249  }
250  os << "}";
251  return os;
252  }
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:176
Identifiers::pop_front
void pop_front()
Definition: identifiers.hpp:84
Identifiers::operator-
friend Identifiers< T > operator-(const Identifiers< T > &lhs, const Identifiers< T > &rhs)
Definition: identifiers.hpp:208
Identifiers::m_ids
std::set< T > m_ids
Definition: identifiers.hpp:93
Identifiers::begin
iterator begin()
Definition: identifiers.hpp:87
Identifiers::size
size_t size() const
Definition: identifiers.hpp:77
Identifiers::clear
void clear()
Definition: identifiers.hpp:86
Identifiers::operator+
friend Identifiers< T > operator+(const Identifiers< T > &lhs, const Identifiers< T > &rhs)
set UNION set
Definition: identifiers.hpp:121
Identifiers::operator*
friend Identifiers< T > operator*(const Identifiers< T > &lhs, const Identifiers< T > &rhs)
set INTERSECTION
Definition: identifiers.hpp:161
Identifiers::begin
const_iterator begin() const
Definition: identifiers.hpp:81
Identifiers::empty
bool empty() const
Definition: identifiers.hpp:78
Identifiers::operator+=
Identifiers< T > & operator+=(const Identifiers< T > &other)
compound set UNION set
Definition: identifiers.hpp:133
Identifiers::back
T back() const
Definition: identifiers.hpp:80
Identifiers::iterator
std::set< T >::iterator iterator
Definition: identifiers.hpp:53
Identifiers::front
T front() const
Definition: identifiers.hpp:79
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:100
Identifiers::operator<<
friend std::ostream & operator<<(std::ostream &os, const Identifiers< T > &identifiers)
Prints the set of identifiers.
Definition: identifiers.hpp:245
Identifiers::operator==
bool operator==(const Identifiers< T > &rhs) const
true when both sets are equal
Definition: identifiers.hpp:109
Identifiers::end
iterator end()
Definition: identifiers.hpp:88
Identifiers::operator-=
Identifiers< T > & operator-=(const Identifiers< T > &other)
compound set DIFFERENCE set
Definition: identifiers.hpp:226
Identifiers::const_iterator
std::set< T >::const_iterator const_iterator
Definition: identifiers.hpp:54
Identifiers
Definition: identifiers.hpp:51