Open Lighting Architecture  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UIDSet.h
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * UIDSet.h
17  * A Set of UIDs
18  * Copyright (C) 2005 Simon Newton
19  */
20 
29 #ifndef INCLUDE_OLA_RDM_UIDSET_H_
30 #define INCLUDE_OLA_RDM_UIDSET_H_
31 
32 #include <ola/rdm/UID.h>
33 #include <algorithm>
34 #include <iomanip>
35 #include <set>
36 #include <string>
37 
38 namespace ola {
39 namespace rdm {
40 
48 class UIDSet {
49  public:
53  typedef std::set<UID>::const_iterator Iterator;
54 
58  UIDSet() {}
59 
64  UIDSet(const UIDSet &other):
65  m_uids(other.m_uids) {
66  }
67 
71  UIDSet& operator=(const UIDSet &other) {
72  if (this != &other) {
73  m_uids = other.m_uids;
74  }
75  return *this;
76  }
77 
81  void Clear() {
82  m_uids.clear();
83  }
84 
89  unsigned int Size() const {
90  return m_uids.size();
91  }
92 
97  void AddUID(const UID &uid) {
98  m_uids.insert(uid);
99  }
100 
105  void RemoveUID(const UID &uid) {
106  m_uids.erase(uid);
107  }
108 
114  bool Contains(const UID &uid) const {
115  return m_uids.find(uid) != m_uids.end();
116  }
117 
123  UIDSet Union(const UIDSet &other) {
124  std::set<UID> result;
125  set_union(m_uids.begin(),
126  m_uids.end(),
127  other.Begin(),
128  other.End(),
129  inserter(result, result.begin()));
130  return UIDSet(result);
131  }
132 
136  Iterator Begin() const {
137  return m_uids.begin();
138  }
139 
143  Iterator End() const {
144  return m_uids.end();
145  }
146 
152  UIDSet SetDifference(const UIDSet &other) {
153  std::set<UID> difference;
154  std::set_difference(m_uids.begin(),
155  m_uids.end(),
156  other.m_uids.begin(),
157  other.m_uids.end(),
158  std::inserter(difference, difference.begin()));
159  return UIDSet(difference);
160  }
161 
166  bool operator==(const UIDSet &other) const {
167  return m_uids == other.m_uids;
168  }
169 
174  bool operator!=(const UIDSet &other) const {
175  return !(*this == other);
176  }
177 
182  std::string ToString() const {
183  std::ostringstream str;
184  std::set<UID>::const_iterator iter;
185  for (iter = m_uids.begin(); iter != m_uids.end(); ++iter) {
186  if (iter != m_uids.begin())
187  str << ",";
188  str << *iter;
189  }
190  return str.str();
191  }
192 
198  friend std::ostream& operator<< (std::ostream &out, const UIDSet &uid_set) {
199  return out << uid_set.ToString();
200  }
201 
202  private:
203  std::set<UID> m_uids;
204 
205  explicit UIDSet(const std::set<UID> uids) {
206  m_uids = uids;
207  }
208 };
209 } // namespace rdm
210 } // namespace ola
211 #endif // INCLUDE_OLA_RDM_UIDSET_H_