libDAI
smallset.h
Go to the documentation of this file.
1 /* This file is part of libDAI - http://www.libdai.org/
2  *
3  * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4  *
5  * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6  */
7 
8 
11 
12 
13 #ifndef __defined_libdai_smallset_h
14 #define __defined_libdai_smallset_h
15 
16 
17 #include <vector>
18 #include <algorithm>
19 #include <iostream>
20 #include <sstream>
21 
22 
23 namespace dai {
24 
25 
27 
31 template <typename T>
32 class SmallSet {
33  private:
35  std::vector<T> _elements;
36 
37  public:
39 
40  SmallSet() : _elements() {}
42 
44  SmallSet( const T &t ) : _elements() {
45  _elements.push_back( t );
46  }
47 
49  SmallSet( const T &t1, const T &t2 ) {
50  if( t1 < t2 ) {
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 );
56  } else
57  _elements.push_back( t1 );
58  }
59 
61 
67  template <typename TIterator>
68  SmallSet( TIterator begin, TIterator end, size_t sizeHint ) {
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() );
74  }
76 
78 
79  SmallSet& insert( const T& t ) {
81  typename SmallSet<T>::iterator it = std::lower_bound( _elements.begin(), _elements.end(), t );
82  if( (it == _elements.end()) || (*it != t) )
83  _elements.insert( it, t );
84  return *this;
85  }
86 
88  SmallSet& erase( const T& t ) {
89  return (*this /= t);
90  }
91 
93  SmallSet operator/ ( const SmallSet& x ) const {
94  SmallSet res;
95  std::set_difference( _elements.begin(), _elements.end(), x._elements.begin(), x._elements.end(), inserter( res._elements, res._elements.begin() ) );
96  return res;
97  }
98 
100  SmallSet operator| ( const SmallSet& x ) const {
101  SmallSet res;
102  std::set_union( _elements.begin(), _elements.end(), x._elements.begin(), x._elements.end(), inserter( res._elements, res._elements.begin() ) );
103  return res;
104  }
105 
107  SmallSet operator& ( const SmallSet& x ) const {
108  SmallSet res;
109  std::set_intersection( _elements.begin(), _elements.end(), x._elements.begin(), x._elements.end(), inserter( res._elements, res._elements.begin() ) );
110  return res;
111  }
112 
115  return (*this = (*this / x));
116  }
117 
119  SmallSet& operator/= ( const T &t ) {
120  typename std::vector<T>::iterator pos = lower_bound( _elements.begin(), _elements.end(), t );
121  if( pos != _elements.end() )
122  if( *pos == t ) // found element, delete it
123  _elements.erase( pos );
124  return *this;
125  }
126 
129  return( *this = (*this | x) );
130  }
131 
133  SmallSet& operator|= ( const T& t ) {
134  typename std::vector<T>::iterator pos = lower_bound( _elements.begin(), _elements.end(), t );
135  if( pos == _elements.end() || *pos != t ) // insert it
136  _elements.insert( pos, t );
137  return *this;
138  }
139 
142  return (*this = (*this & x));
143  }
144 
146  bool operator<< ( const SmallSet& x ) const {
147  return std::includes( x._elements.begin(), x._elements.end(), _elements.begin(), _elements.end() );
148  }
149 
151  bool operator>> ( const SmallSet& x ) const {
152  return std::includes( _elements.begin(), _elements.end(), x._elements.begin(), x._elements.end() );
153  }
155 
157 
158  bool intersects( const SmallSet& x ) const {
160  return( (*this & x).size() > 0 );
161  }
162 
164  bool contains( const T &t ) const {
165  return std::binary_search( _elements.begin(), _elements.end(), t );
166  }
167 
169  typename std::vector<T>::size_type size() const { return _elements.size(); }
170 
172  bool empty() const { return _elements.size() == 0; }
173 
175  std::vector<T>& elements() { return _elements; }
176 
178  const std::vector<T>& elements() const { return _elements; }
180 
182  typedef typename std::vector<T>::const_iterator const_iterator;
184  typedef typename std::vector<T>::iterator iterator;
186  typedef typename std::vector<T>::const_reverse_iterator const_reverse_iterator;
188  typedef typename std::vector<T>::reverse_iterator reverse_iterator;
189 
191 
192  iterator begin() { return _elements.begin(); }
195  const_iterator begin() const { return _elements.begin(); }
196 
198  iterator end() { return _elements.end(); }
200  const_iterator end() const { return _elements.end(); }
201 
203  reverse_iterator rbegin() { return _elements.rbegin(); }
205  const_reverse_iterator rbegin() const { return _elements.rbegin(); }
206 
208  reverse_iterator rend() { return _elements.rend(); }
210  const_reverse_iterator rend() const { return _elements.rend(); }
211 
213  T& front() { return _elements.front(); }
215  const T& front() const { return _elements.front(); }
216 
218  T& back() { return _elements.back(); }
220  const T& back() const { return _elements.back(); }
222 
224 
225  friend bool operator==( const SmallSet &a, const SmallSet &b ) {
227  return (a._elements == b._elements);
228  }
229 
231  friend bool operator!=( const SmallSet &a, const SmallSet &b ) {
232  return !(a._elements == b._elements);
233  }
234 
236  friend bool operator<( const SmallSet &a, const SmallSet &b ) {
237  return a._elements < b._elements;
238  }
240 
242 
243  friend std::ostream& operator << ( std::ostream& os, const SmallSet& x ) {
245  os << "{";
246  for( typename std::vector<T>::const_iterator it = x.begin(); it != x.end(); it++ )
247  os << (it != x.begin() ? ", " : "") << *it;
248  os << "}";
249  return os;
250  }
251 
253  std::string toString() const {
254  std::stringstream ss;
255  ss << *this;
256  return ss.str();
257  }
259 };
260 
261 
262 } // end of namespace dai
263 
264 
265 #endif
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