ae8cdd0dac9b74f0df2e4c761475e275e883d8be
[libdai.git] / include / dai / smallset.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2002 Martijn Leisink [martijn@mbfys.kun.nl]
8 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
9 * Copyright (C) 2002-2007 Radboud University Nijmegen, The Netherlands
10 */
11
12
13 /// \file
14 /// \brief Defines the SmallSet<> class, which represents a set (optimized for a small number of elements).
15
16
17 #ifndef __defined_libdai_smallset_h
18 #define __defined_libdai_smallset_h
19
20
21 #include <vector>
22 #include <algorithm>
23 #include <iostream>
24
25
26 namespace dai {
27
28
29 /// Represents a set; the implementation is optimized for a small number of elements.
30 /** SmallSet uses an ordered <tt>std::vector<</tt><em>T</em><tt>></tt> to represent a set; this is faster than
31 * using a <tt>std::set<</tt><em>T</em><tt>></tt> if the number of elements is small.
32 * \tparam T Should be less-than-comparable.
33 */
34 template <typename T>
35 class SmallSet {
36 private:
37 /// The elements in this set
38 std::vector<T> _elements;
39
40 public:
41 /// \name Constructors and destructors
42 //@{
43 /// Default constructor (constructs an empty set)
44 SmallSet() : _elements() {}
45
46 /// Construct a set consisting of one element
47 SmallSet( const T &t ) : _elements() {
48 _elements.push_back( t );
49 }
50
51 /// Construct a set consisting of two elements
52 SmallSet( const T &t1, const T &t2 ) {
53 if( t1 < t2 ) {
54 _elements.push_back( t1 );
55 _elements.push_back( t2 );
56 } else if( t2 < t1 ) {
57 _elements.push_back( t2 );
58 _elements.push_back( t1 );
59 } else
60 _elements.push_back( t1 );
61 }
62
63 /// Construct a SmallSet from a range of elements.
64 /** \tparam TIterator Iterates over instances of type \a T.
65 * \param begin Points to first element to be added.
66 * \param end Points just beyond last element to be added.
67 * \param sizeHint For efficiency, the number of elements can be speficied by \a sizeHint.
68 */
69 template <typename TIterator>
70 SmallSet( TIterator begin, TIterator end, size_t sizeHint=0 ) {
71 _elements.reserve( sizeHint );
72 _elements.insert( _elements.begin(), begin, end );
73 std::sort( _elements.begin(), _elements.end() );
74 typename std::vector<T>::iterator new_end = std::unique( _elements.begin(), _elements.end() );
75 _elements.erase( new_end, _elements.end() );
76 }
77 //@}
78
79 /// \name Operators for set-theoretic operations
80 //@{
81 /// Set-minus operator: returns all elements in \c *this, except those in \a x
82 SmallSet operator/ ( const SmallSet& x ) const {
83 SmallSet res;
84 std::set_difference( _elements.begin(), _elements.end(), x._elements.begin(), x._elements.end(), inserter( res._elements, res._elements.begin() ) );
85 return res;
86 }
87
88 /// Set-union operator: returns all elements in \c *this, plus those in \a x
89 SmallSet operator| ( const SmallSet& x ) const {
90 SmallSet res;
91 std::set_union( _elements.begin(), _elements.end(), x._elements.begin(), x._elements.end(), inserter( res._elements, res._elements.begin() ) );
92 return res;
93 }
94
95 /// Set-intersection operator: returns all elements in \c *this that are also contained in \a x
96 SmallSet operator& ( const SmallSet& x ) const {
97 SmallSet res;
98 std::set_intersection( _elements.begin(), _elements.end(), x._elements.begin(), x._elements.end(), inserter( res._elements, res._elements.begin() ) );
99 return res;
100 }
101
102 /// Erases from \c *this all elements in \a x
103 SmallSet& operator/= ( const SmallSet& x ) {
104 return (*this = (*this / x));
105 }
106
107 /// Erases one element
108 SmallSet& operator/= ( const T &t ) {
109 typename std::vector<T>::iterator pos = lower_bound( _elements.begin(), _elements.end(), t );
110 if( pos != _elements.end() )
111 if( *pos == t ) // found element, delete it
112 _elements.erase( pos );
113 return *this;
114 }
115
116 /// Adds to \c *this all elements in \a x
117 SmallSet& operator|= ( const SmallSet& x ) {
118 return( *this = (*this | x) );
119 }
120
121 /// Adds one element
122 SmallSet& operator|= ( const T& t ) {
123 typename std::vector<T>::iterator pos = lower_bound( _elements.begin(), _elements.end(), t );
124 if( pos == _elements.end() || *pos != t ) // insert it
125 _elements.insert( pos, t );
126 return *this;
127 }
128
129 /// Erases from \c *this all elements not in \a x
130 SmallSet& operator&= ( const SmallSet& x ) {
131 return (*this = (*this & x));
132 }
133
134 /// Returns \c true if \c *this is a subset of \a x
135 bool operator<< ( const SmallSet& x ) const {
136 return std::includes( x._elements.begin(), x._elements.end(), _elements.begin(), _elements.end() );
137 }
138
139 /// Returns \c true if \a x is a subset of \c *this
140 bool operator>> ( const SmallSet& x ) const {
141 return std::includes( _elements.begin(), _elements.end(), x._elements.begin(), x._elements.end() );
142 }
143 //@}
144
145 /// \name Queries
146 //@{
147 /// Returns \c true if \c *this and \a x have elements in common
148 bool intersects( const SmallSet& x ) const {
149 return( (*this & x).size() > 0 );
150 }
151
152 /// Returns \c true if \c *this contains the element \a t
153 bool contains( const T &t ) const {
154 return std::binary_search( _elements.begin(), _elements.end(), t );
155 }
156
157 /// Returns number of elements
158 typename std::vector<T>::size_type size() const { return _elements.size(); }
159
160 /// Returns whether \c *this is empty
161 bool empty() const { return _elements.size() == 0; }
162
163 /// Returns reference to the elements
164 std::vector<T>& elements() { return _elements; }
165
166 /// Returns constant reference to the elements
167 const std::vector<T>& elements() const { return _elements; }
168 //@}
169
170 /// Constant iterator over the elements
171 typedef typename std::vector<T>::const_iterator const_iterator;
172 /// Iterator over the elements
173 typedef typename std::vector<T>::iterator iterator;
174 /// Constant reverse iterator over the elements
175 typedef typename std::vector<T>::const_reverse_iterator const_reverse_iterator;
176 /// Reverse iterator over the elements
177 typedef typename std::vector<T>::reverse_iterator reverse_iterator;
178
179 /// \name Iterator interface
180 //@{
181 /// Returns iterator that points to the first element
182 iterator begin() { return _elements.begin(); }
183 /// Returns constant iterator that points to the first element
184 const_iterator begin() const { return _elements.begin(); }
185
186 /// Returns iterator that points beyond the last element
187 iterator end() { return _elements.end(); }
188 /// Returns constant iterator that points beyond the last element
189 const_iterator end() const { return _elements.end(); }
190
191 /// Returns reverse iterator that points to the last element
192 reverse_iterator rbegin() { return _elements.rbegin(); }
193 /// Returns constant reverse iterator that points to the last element
194 const_reverse_iterator rbegin() const { return _elements.rbegin(); }
195
196 /// Returns reverse iterator that points beyond the first element
197 reverse_iterator rend() { return _elements.rend(); }
198 /// Returns constant reverse iterator that points beyond the first element
199 const_reverse_iterator rend() const { return _elements.rend(); }
200
201 /// Returns reference to first element
202 T& front() { return _elements.front(); }
203 /// Returns constant reference to first element
204 const T& front() const { return _elements.front(); }
205
206 /// Returns reference to last element
207 T& back() { return _elements.back(); }
208 /// Returns constant reference to last element
209 const T& back() const { return _elements.back(); }
210 //@}
211
212 /// \name Comparison operators
213 //@{
214 /// Returns \c true if \a a and \a b are identical
215 friend bool operator==( const SmallSet &a, const SmallSet &b ) {
216 return (a._elements == b._elements);
217 }
218
219 /// Returns \c true if \a a and \a b are not identical
220 friend bool operator!=( const SmallSet &a, const SmallSet &b ) {
221 return !(a._elements == b._elements);
222 }
223
224 /// Lexicographical comparison of elements
225 friend bool operator<( const SmallSet &a, const SmallSet &b ) {
226 return a._elements < b._elements;
227 }
228 //@}
229
230 /// \name Streaming input/output
231 //@{
232 /// Writes a SmallSet to an output stream
233 friend std::ostream& operator << ( std::ostream& os, const SmallSet& x ) {
234 os << "{";
235 for( typename std::vector<T>::const_iterator it = x.begin(); it != x.end(); it++ )
236 os << (it != x.begin() ? ", " : "") << *it;
237 os << "}";
238 return os;
239 }
240 //@}
241 };
242
243
244 } // end of namespace dai
245
246
247 #endif