13 #ifndef __defined_libdai_smallset_h
14 #define __defined_libdai_smallset_h
45 _elements.push_back( t );
51 _elements.push_back( t1 );
52 _elements.push_back( t2 );
53 }
else if( t2 < t1 ) {
54 _elements.push_back( t2 );
55 _elements.push_back( t1 );
57 _elements.push_back( t1 );
67 template <
typename TIterator>
69 _elements.reserve( sizeHint );
70 _elements.insert( _elements.begin(),
begin,
end );
71 std::sort( _elements.begin(), _elements.end() );
72 typename std::vector<T>::iterator new_end = std::unique( _elements.begin(), _elements.end() );
73 _elements.erase( new_end, _elements.end() );
82 if( (it == _elements.end()) || (*it != t) )
83 _elements.insert( it, t );
115 return (*
this = (*
this / x));
120 typename std::vector<T>::iterator pos = lower_bound( _elements.begin(), _elements.end(), t );
121 if( pos != _elements.end() )
123 _elements.erase( pos );
129 return( *
this = (*
this | x) );
134 typename std::vector<T>::iterator pos = lower_bound( _elements.begin(), _elements.end(), t );
135 if( pos == _elements.end() || *pos != t )
136 _elements.insert( pos, t );
142 return (*
this = (*
this & x));
147 return std::includes( x.
_elements.begin(), x.
_elements.end(), _elements.begin(), _elements.end() );
152 return std::includes( _elements.begin(), _elements.end(), x.
_elements.begin(), x.
_elements.end() );
160 return( (*
this & x).
size() > 0 );
165 return std::binary_search( _elements.begin(), _elements.end(), t );
169 typename std::vector<T>::size_type
size()
const {
return _elements.size(); }
172 bool empty()
const {
return _elements.size() == 0; }
184 typedef typename std::vector<T>::iterator
iterator;
192 iterator
begin() {
return _elements.begin(); }
195 const_iterator
begin()
const {
return _elements.begin(); }
198 iterator
end() {
return _elements.end(); }
200 const_iterator
end()
const {
return _elements.end(); }
203 reverse_iterator
rbegin() {
return _elements.rbegin(); }
205 const_reverse_iterator
rbegin()
const {
return _elements.rbegin(); }
208 reverse_iterator
rend() {
return _elements.rend(); }
210 const_reverse_iterator
rend()
const {
return _elements.rend(); }
213 T&
front() {
return _elements.front(); }
215 const T&
front()
const {
return _elements.front(); }
218 T&
back() {
return _elements.back(); }
220 const T&
back()
const {
return _elements.back(); }
246 for(
typename std::vector<T>::const_iterator it = x.
begin(); it != x.
end(); it++ )
247 os << (it != x.
begin() ?
", " :
"") << *it;
254 std::stringstream ss;
const T & back() const
Returns constant reference to last element.
Definition: smallset.h:220
iterator end()
Returns iterator that points beyond the last element.
Definition: smallset.h:198
SmallSet & operator|=(const SmallSet &x)
Adds to *this all elements in x.
Definition: smallset.h:128
friend bool operator!=(const SmallSet &a, const SmallSet &b)
Returns true if a and b are not identical.
Definition: smallset.h:231
SmallSet(const T &t)
Construct a set consisting of one element.
Definition: smallset.h:44
friend bool operator==(const SmallSet &a, const SmallSet &b)
Returns true if a and b are identical.
Definition: smallset.h:226
SmallSet()
Default constructor (constructs an empty set)
Definition: smallset.h:41
std::vector< T > _elements
The elements in this set.
Definition: smallset.h:35
bool intersects(const SmallSet &x) const
Returns true if *this and x have elements in common.
Definition: smallset.h:159
std::vector< T >::size_type size() const
Returns number of elements.
Definition: smallset.h:169
iterator begin()
Returns iterator that points to the first element.
Definition: smallset.h:193
SmallSet & insert(const T &t)
Inserts t into *this.
Definition: smallset.h:80
std::vector< T >::iterator iterator
Iterator over the elements.
Definition: smallset.h:184
SmallSet operator&(const SmallSet &x) const
Set-intersection operator: returns all elements in *this that are also contained in x...
Definition: smallset.h:107
const_iterator begin() const
Returns constant iterator that points to the first element.
Definition: smallset.h:195
SmallSet(TIterator begin, TIterator end, size_t sizeHint)
Construct a SmallSet from a range of elements.
Definition: smallset.h:68
const_reverse_iterator rend() const
Returns constant reverse iterator that points beyond the first element.
Definition: smallset.h:210
reverse_iterator rend()
Returns reverse iterator that points beyond the first element.
Definition: smallset.h:208
bool contains(const T &t) const
Returns true if *this contains the element t.
Definition: smallset.h:164
const_iterator end() const
Returns constant iterator that points beyond the last element.
Definition: smallset.h:200
std::vector< T >::const_iterator const_iterator
Constant iterator over the elements.
Definition: smallset.h:182
SmallSet & operator/=(const SmallSet &x)
Erases from *this all elements in x.
Definition: smallset.h:114
T & back()
Returns reference to last element.
Definition: smallset.h:218
std::vector< T >::const_reverse_iterator const_reverse_iterator
Constant reverse iterator over the elements.
Definition: smallset.h:186
friend bool operator<(const SmallSet &a, const SmallSet &b)
Lexicographical comparison of elements.
Definition: smallset.h:236
std::vector< T >::reverse_iterator reverse_iterator
Reverse iterator over the elements.
Definition: smallset.h:188
const_reverse_iterator rbegin() const
Returns constant reverse iterator that points to the last element.
Definition: smallset.h:205
SmallSet operator|(const SmallSet &x) const
Set-union operator: returns all elements in *this, plus those in x.
Definition: smallset.h:100
SmallSet & operator&=(const SmallSet &x)
Erases from *this all elements not in x.
Definition: smallset.h:141
const T & front() const
Returns constant reference to first element.
Definition: smallset.h:215
const std::vector< T > & elements() const
Returns constant reference to the elements.
Definition: smallset.h:178
SmallSet & erase(const T &t)
Erases t from *this.
Definition: smallset.h:88
Namespace for libDAI.
Definition: alldai.cpp:16
std::vector< T > & elements()
Returns reference to the elements.
Definition: smallset.h:175
T & front()
Returns reference to first element.
Definition: smallset.h:213
SmallSet operator/(const SmallSet &x) const
Set-minus operator: returns all elements in *this, except those in x.
Definition: smallset.h:93
std::string toString() const
Formats a SmallSet as a string.
Definition: smallset.h:253
Represents a set; the implementation is optimized for a small number of elements. ...
Definition: smallset.h:32
reverse_iterator rbegin()
Returns reverse iterator that points to the last element.
Definition: smallset.h:203
SmallSet(const T &t1, const T &t2)
Construct a set consisting of two elements.
Definition: smallset.h:49
bool operator<<(const SmallSet &x) const
Returns true if *this is a subset of x.
Definition: smallset.h:146
bool empty() const
Returns whether *this is empty.
Definition: smallset.h:172
bool operator>>(const SmallSet &x) const
Returns true if x is a subset of *this.
Definition: smallset.h:151