Fixed some bugs and deprecated non-const operator[] of Prob,Factor
[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 * \note The \a sizeHint parameter used to be optional in libDAI versions 0.2.4 and earlier.
69 */
70 template <typename TIterator>
71 SmallSet( TIterator begin, TIterator end, size_t sizeHint ) {
72 _elements.reserve( sizeHint );
73 _elements.insert( _elements.begin(), begin, end );
74 std::sort( _elements.begin(), _elements.end() );
75 typename std::vector<T>::iterator new_end = std::unique( _elements.begin(), _elements.end() );
76 _elements.erase( new_end, _elements.end() );
77 }
78 //@}
79
80 /// \name Operators for set-theoretic operations
81 //@{
82 /// Set-minus operator: returns all elements in \c *this, except those in \a x
83 SmallSet operator/ ( const SmallSet& x ) const {
84 SmallSet res;
85 std::set_difference( _elements.begin(), _elements.end(), x._elements.begin(), x._elements.end(), inserter( res._elements, res._elements.begin() ) );
86 return res;
87 }
88
89 /// Set-union operator: returns all elements in \c *this, plus those in \a x
90 SmallSet operator| ( const SmallSet& x ) const {
91 SmallSet res;
92 std::set_union( _elements.begin(), _elements.end(), x._elements.begin(), x._elements.end(), inserter( res._elements, res._elements.begin() ) );
93 return res;
94 }
95
96 /// Set-intersection operator: returns all elements in \c *this that are also contained in \a x
97 SmallSet operator& ( const SmallSet& x ) const {
98 SmallSet res;
99 std::set_intersection( _elements.begin(), _elements.end(), x._elements.begin(), x._elements.end(), inserter( res._elements, res._elements.begin() ) );
100 return res;
101 }
102
103 /// Erases from \c *this all elements in \a x
104 SmallSet& operator/= ( const SmallSet& x ) {
105 return (*this = (*this / x));
106 }
107
108 /// Erases one element
109 SmallSet& operator/= ( const T &t ) {
110 typename std::vector<T>::iterator pos = lower_bound( _elements.begin(), _elements.end(), t );
111 if( pos != _elements.end() )
112 if( *pos == t ) // found element, delete it
113 _elements.erase( pos );
114 return *this;
115 }
116
117 /// Adds to \c *this all elements in \a x
118 SmallSet& operator|= ( const SmallSet& x ) {
119 return( *this = (*this | x) );
120 }
121
122 /// Adds one element
123 SmallSet& operator|= ( const T& t ) {
124 typename std::vector<T>::iterator pos = lower_bound( _elements.begin(), _elements.end(), t );
125 if( pos == _elements.end() || *pos != t ) // insert it
126 _elements.insert( pos, t );
127 return *this;
128 }
129
130 /// Erases from \c *this all elements not in \a x
131 SmallSet& operator&= ( const SmallSet& x ) {
132 return (*this = (*this & x));
133 }
134
135 /// Returns \c true if \c *this is a subset of \a x
136 bool operator<< ( const SmallSet& x ) const {
137 return std::includes( x._elements.begin(), x._elements.end(), _elements.begin(), _elements.end() );
138 }
139
140 /// Returns \c true if \a x is a subset of \c *this
141 bool operator>> ( const SmallSet& x ) const {
142 return std::includes( _elements.begin(), _elements.end(), x._elements.begin(), x._elements.end() );
143 }
144 //@}
145
146 /// \name Queries
147 //@{
148 /// Returns \c true if \c *this and \a x have elements in common
149 bool intersects( const SmallSet& x ) const {
150 return( (*this & x).size() > 0 );
151 }
152
153 /// Returns \c true if \c *this contains the element \a t
154 bool contains( const T &t ) const {
155 return std::binary_search( _elements.begin(), _elements.end(), t );
156 }
157
158 /// Returns number of elements
159 typename std::vector<T>::size_type size() const { return _elements.size(); }
160
161 /// Returns whether \c *this is empty
162 bool empty() const { return _elements.size() == 0; }
163
164 /// Returns reference to the elements
165 std::vector<T>& elements() { return _elements; }
166
167 /// Returns constant reference to the elements
168 const std::vector<T>& elements() const { return _elements; }
169 //@}
170
171 /// Constant iterator over the elements
172 typedef typename std::vector<T>::const_iterator const_iterator;
173 /// Iterator over the elements
174 typedef typename std::vector<T>::iterator iterator;
175 /// Constant reverse iterator over the elements
176 typedef typename std::vector<T>::const_reverse_iterator const_reverse_iterator;
177 /// Reverse iterator over the elements
178 typedef typename std::vector<T>::reverse_iterator reverse_iterator;
179
180 /// \name Iterator interface
181 //@{
182 /// Returns iterator that points to the first element
183 iterator begin() { return _elements.begin(); }
184 /// Returns constant iterator that points to the first element
185 const_iterator begin() const { return _elements.begin(); }
186
187 /// Returns iterator that points beyond the last element
188 iterator end() { return _elements.end(); }
189 /// Returns constant iterator that points beyond the last element
190 const_iterator end() const { return _elements.end(); }
191
192 /// Returns reverse iterator that points to the last element
193 reverse_iterator rbegin() { return _elements.rbegin(); }
194 /// Returns constant reverse iterator that points to the last element
195 const_reverse_iterator rbegin() const { return _elements.rbegin(); }
196
197 /// Returns reverse iterator that points beyond the first element
198 reverse_iterator rend() { return _elements.rend(); }
199 /// Returns constant reverse iterator that points beyond the first element
200 const_reverse_iterator rend() const { return _elements.rend(); }
201
202 /// Returns reference to first element
203 T& front() { return _elements.front(); }
204 /// Returns constant reference to first element
205 const T& front() const { return _elements.front(); }
206
207 /// Returns reference to last element
208 T& back() { return _elements.back(); }
209 /// Returns constant reference to last element
210 const T& back() const { return _elements.back(); }
211 //@}
212
213 /// \name Comparison operators
214 //@{
215 /// Returns \c true if \a a and \a b are identical
216 friend bool operator==( const SmallSet &a, const SmallSet &b ) {
217 return (a._elements == b._elements);
218 }
219
220 /// Returns \c true if \a a and \a b are not identical
221 friend bool operator!=( const SmallSet &a, const SmallSet &b ) {
222 return !(a._elements == b._elements);
223 }
224
225 /// Lexicographical comparison of elements
226 friend bool operator<( const SmallSet &a, const SmallSet &b ) {
227 return a._elements < b._elements;
228 }
229 //@}
230
231 /// \name Streaming input/output
232 //@{
233 /// Writes a SmallSet to an output stream
234 friend std::ostream& operator << ( std::ostream& os, const SmallSet& x ) {
235 os << "{";
236 for( typename std::vector<T>::const_iterator it = x.begin(); it != x.end(); it++ )
237 os << (it != x.begin() ? ", " : "") << *it;
238 os << "}";
239 return os;
240 }
241 //@}
242 };
243
244
245 } // end of namespace dai
246
247
248 #endif