PGROUTING  2.6-dev
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 
32 #ifndef INCLUDE_CPP_COMMON_IDENTIFIERS_HPP_
33 #define INCLUDE_CPP_COMMON_IDENTIFIERS_HPP_
34 #pragma once
35 
36 #include <set>
37 #include <algorithm>
38 #include <iterator>
39 #include <sstream>
40 #include <iostream>
41 #include <stdexcept>
42 
43 /* TODO(vicky)
44  * compiler check that type T is a integral type
45  */
46 
47 template <typename T>
48 class Identifiers {
49  public:
50  typedef typename std::set<T>::iterator iterator;
51  typedef typename std::set<T>::const_iterator const_iterator;
52 
53 
55 
56  Identifiers<T>() = default;
57  Identifiers<T>(const Identifiers<T>&) = default;
58  Identifiers<T>(const std::set<T>& data) {
59  m_ids = data;
60  }
61 
62  /* @brief initializes with {1 ~ number}
63  *
64  * @params [in] number
65  */
66  explicit Identifiers<T>(const size_t number) {
67  size_t i(0);
68  std::generate_n(std::inserter(m_ids, m_ids.begin()),
69  number,
70  [&i](){ return i++; });
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  const_iterator begin() const {return m_ids.begin();}
81  const_iterator end() const {return m_ids.end();}
82  inline void pop_front() {m_ids.erase(m_ids.begin());}
83  inline void clear() {m_ids.clear();}
84  iterator begin() {return m_ids.begin();}
85  iterator end() {return m_ids.end();}
87 
88 
89  private:
90  std::set<T> m_ids;
91 
92  public:
94 
97  bool has(const T other) const {
98  return (m_ids.find(other) != m_ids.end());
99  }
100 
101 
103 
106  bool operator==(const Identifiers<T> &rhs) const {
107  return std::equal(m_ids.begin(), m_ids.end(), rhs.m_ids.begin());
108  }
109 
112 
119  const Identifiers<T> &lhs,
120  const Identifiers<T> &rhs) {
121  Identifiers<T> union_ids(lhs);
122  union_ids += rhs;
123  return union_ids;
124  }
125 
127 
131  const Identifiers<T> &other) {
132  m_ids.insert(other.m_ids.begin(), other.m_ids.end());
133  return *this;
134  }
136 
139  Identifiers<T>& operator +=(const T &element) {
140  m_ids.insert(element);
141  return *this;
142  }
143 
145 
146 
147 
150 
159  const Identifiers<T> &lhs,
160  const Identifiers<T> &rhs) {
161  std::set<T> result;
162  std::set_intersection(
163  lhs.m_ids.begin(), lhs.m_ids.end(),
164  rhs.m_ids.begin(), rhs.m_ids.end(),
165  std::inserter(result, result.begin()));
166  return Identifiers<T>(result);
167  }
168 
170 
174  const Identifiers<T> &other) {
175  *this = *this * other;
176  return *this;
177  }
178 
180 
183  Identifiers<T>& operator *=(const T &element) {
184  if (has(element)) {
185  m_ids.clear();
186  m_ids.insert(element);
187  } else {
188  m_ids.clear();
189  }
190  return *this;
191  }
192 
194 
195 
198 
199  /* \brief set DIFFERENCE set
200  *
201  * @param[in] lhs Identifiers
202  * @param[in] rhs Identifiers
203  */
204  friend
206  const Identifiers<T> &lhs,
207  const Identifiers<T> &rhs) {
208  std::set<T> result;
209  std::set_difference(
210  lhs.m_ids.begin(), lhs.m_ids.end(),
211  rhs.m_ids.begin(), rhs.m_ids.end(),
212  std::inserter(result, result.begin()));
213  return Identifiers<T>(result);
214  }
215 
216 
217 
219 
224  *this = *this - other;
225  return *this;
226  }
227 
229 
232  Identifiers<T>& operator -=(const T &element) {
233  m_ids.erase(element);
234  return *this;
235  }
236 
238 
240  friend
241  std::ostream&
242  operator<<(std::ostream& os, const Identifiers<T>& identifiers) {
243  os << "{";
244  for (auto identifier : identifiers.m_ids) {
245  os << identifier << ", ";
246  }
247  os << "}";
248  return os;
249  }
250 
252 };
253 
254 #endif // INCLUDE_CPP_COMMON_IDENTIFIERS_HPP_
bool has(const T other) const
true ids() has element
Definition: identifiers.hpp:97
Identifiers< T > & operator-=(const Identifiers< T > &other)
compound set DIFFERENCE set
void pop_front()
Definition: identifiers.hpp:82
friend Identifiers< T > operator-(const Identifiers< T > &lhs, const Identifiers< T > &rhs)
bool operator==(const Identifiers< T > &rhs) const
true when both sets are equal
std::set< T > m_ids
Definition: identifiers.hpp:90
Identifiers< T > & operator*=(const Identifiers< T > &other)
coumpound set INTERSECTION set
const_iterator begin() const
Definition: identifiers.hpp:80
const_iterator end() const
Definition: identifiers.hpp:81
void clear()
Definition: identifiers.hpp:83
friend Identifiers< T > operator+(const Identifiers< T > &lhs, const Identifiers< T > &rhs)
set UNION set
iterator begin()
Definition: identifiers.hpp:84
std::set< T >::iterator iterator
Definition: identifiers.hpp:50
friend Identifiers< T > operator*(const Identifiers< T > &lhs, const Identifiers< T > &rhs)
set INTERSECTION
Identifiers< T > & operator+=(const Identifiers< T > &other)
compound set UNION set
size_t size() const
Definition: identifiers.hpp:77
T front() const
Definition: identifiers.hpp:79
bool empty() const
Definition: identifiers.hpp:78
iterator end()
Definition: identifiers.hpp:85
std::set< T >::const_iterator const_iterator
Definition: identifiers.hpp:51