Open Lighting Architecture
 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * UIDSet.h
17  * A Set of UIDs
18  * Copyright (C) 2005-2010 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 
41 using std::ostream;
42 using std::set;
43 
51 class UIDSet {
52  public:
56  typedef set<UID>::const_iterator Iterator;
57 
61  UIDSet() {}
62 
67  UIDSet(const UIDSet &other):
68  m_uids(other.m_uids) {
69  }
70 
74  UIDSet& operator=(const UIDSet &other) {
75  if (this != &other) {
76  m_uids = other.m_uids;
77  }
78  return *this;
79  }
80 
84  void Clear() {
85  m_uids.clear();
86  }
87 
92  unsigned int Size() const {
93  return m_uids.size();
94  }
95 
100  void AddUID(const UID &uid) {
101  m_uids.insert(uid);
102  }
103 
108  void RemoveUID(const UID &uid) {
109  m_uids.erase(uid);
110  }
111 
117  bool Contains(const UID &uid) const {
118  return m_uids.find(uid) != m_uids.end();
119  }
120 
126  UIDSet Union(const UIDSet &other) {
127  set<UID> result;
128  set_union(m_uids.begin(),
129  m_uids.end(),
130  other.Begin(),
131  other.End(),
132  inserter(result, result.begin()));
133  return UIDSet(result);
134  }
135 
139  Iterator Begin() const {
140  return m_uids.begin();
141  }
142 
146  Iterator End() const {
147  return m_uids.end();
148  }
149 
155  UIDSet SetDifference(const UIDSet &other) {
156  set<UID> difference;
157  std::set_difference(m_uids.begin(),
158  m_uids.end(),
159  other.m_uids.begin(),
160  other.m_uids.end(),
161  std::inserter(difference, difference.begin()));
162  return UIDSet(difference);
163  }
164 
169  bool operator==(const UIDSet &other) const {
170  return m_uids == other.m_uids;
171  }
172 
177  bool operator!=(const UIDSet &other) const {
178  return !(*this == other);
179  }
180 
185  std::string ToString() const {
186  std::stringstream str;
187  set<UID>::const_iterator iter;
188  for (iter = m_uids.begin(); iter != m_uids.end(); ++iter) {
189  if (iter != m_uids.begin())
190  str << ",";
191  str << *iter;
192  }
193  return str.str();
194  }
195 
201  friend ostream& operator<< (ostream &out, const UIDSet &uid_set) {
202  return out << uid_set.ToString();
203  }
204 
205  private:
206  set<UID> m_uids;
207 
208  explicit UIDSet(const set<UID> uids) {
209  m_uids = uids;
210  }
211 };
212 } // namespace rdm
213 } // namespace ola
214 #endif // INCLUDE_OLA_RDM_UIDSET_H_