CMS 3D CMS Logo

Selection.h
Go to the documentation of this file.
1 #ifndef CommonTools_Utils_Selection_h
2 #define CommonTools_Utils_Selection_h
3 #include <vector>
4 
5 template<typename C,
6  typename Selector,
7  typename StoreContainer = std::vector<const typename C::value_type *> >
8 class Selection {
9 public:
10  typedef typename C::value_type value_type;
11  typedef typename C::size_type size_type;
12  typedef value_type & reference;
13  typedef const value_type & const_reference;
14  Selection( const C & c, const Selector & sel ) :
15  select_( sel ) {
16  for( typename C::const_iterator i = c.begin(); i != c.end(); ++i ) {
17  if ( select_( *i ) ) selected_.push_back( & * i );
18  }
19  }
21  public:
23  typedef value_type * pointer;
24  typedef value_type & reference;
25  typedef std::ptrdiff_t difference_type;
26  typedef typename StoreContainer::const_iterator::iterator_category iterator_category;
27  const_iterator(const typename StoreContainer::const_iterator & it) : i(it) { }
28  const_iterator(const const_iterator & it) : i(it.i) { }
30  const_iterator & operator=(const const_iterator & it) { i = it.i; return *this; }
31  const_iterator& operator++() { ++i; return *this; }
32  const_iterator operator++(int) { const_iterator ci = *this; ++i; return ci; }
33  const_iterator& operator--() { --i; return *this; }
34  const_iterator operator--(int) { const_iterator ci = *this; --i; return ci; }
35  difference_type operator-(const const_iterator & o) const { return i - o.i; }
36  const_iterator operator+(difference_type n) const { return const_iterator(i + n); }
37  const_iterator operator-(difference_type n) const { return const_iterator(i - n); }
38  bool operator<(const const_iterator & o) const { return i < o.i; }
39  bool operator==(const const_iterator& ci) const { return i == ci.i; }
40  bool operator!=(const const_iterator& ci) const { return i != ci.i; }
41  const value_type & operator *() const { return **i; }
42  const value_type * operator->() const { return & (operator*()); }
43  const_iterator & operator +=(difference_type d) { i += d; return *this; }
44  const_iterator & operator -=(difference_type d) { i -= d; return *this; }
45  private:
46  typename StoreContainer::const_iterator i;
47  };
48  const_iterator begin() const { return const_iterator( selected_.begin() ); }
49  const_iterator end() const { return const_iterator( selected_.end() ); }
50  size_type size() const { return selected_.size(); }
51  bool empty() const { return selected_.empty(); }
52  const_reference operator[]( size_type i ) { return * selected_[i]; }
53 private:
55  StoreContainer selected_;
56 };
57 
58 #endif
const value_type & const_reference
Definition: Selection.h:13
StoreContainer::const_iterator::iterator_category iterator_category
Definition: Selection.h:26
value_type * pointer
Definition: Selection.h:23
std::ptrdiff_t difference_type
Definition: Selection.h:25
const_iterator operator--(int)
Definition: Selection.h:34
bool operator==(const const_iterator &ci) const
Definition: Selection.h:39
const_iterator & operator+=(difference_type d)
Definition: Selection.h:43
bool operator!=(const const_iterator &ci) const
Definition: Selection.h:40
const_iterator & operator++()
Definition: Selection.h:31
const_iterator(const typename StoreContainer::const_iterator &it)
Definition: Selection.h:27
const_iterator begin() const
Definition: Selection.h:48
const_reference operator[](size_type i)
Definition: Selection.h:52
bool empty() const
Definition: Selection.h:51
size_type size() const
Definition: Selection.h:50
Selection< C, Selector, StoreContainer >::value_type value_type
Definition: Selection.h:22
uint16_t size_type
const_iterator operator-(difference_type n) const
Definition: Selection.h:37
C::value_type value_type
Definition: Selection.h:10
StoreContainer::const_iterator i
Definition: Selection.h:46
const_iterator operator++(int)
Definition: Selection.h:32
Selector select_
Definition: Selection.h:54
value_type & reference
Definition: Selection.h:12
const_iterator & operator-=(difference_type d)
Definition: Selection.h:44
difference_type operator-(const const_iterator &o) const
Definition: Selection.h:35
Functor that operates on <T>
Definition: Selector.h:24
const_iterator operator+(difference_type n) const
Definition: Selection.h:36
value_type & reference
Definition: Selection.h:24
StoreContainer selected_
Definition: Selection.h:55
const_iterator & operator--()
Definition: Selection.h:33
C::size_type size_type
Definition: Selection.h:11
const_iterator(const const_iterator &it)
Definition: Selection.h:28
Selection(const C &c, const Selector &sel)
Definition: Selection.h:14
const value_type * operator->() const
Definition: Selection.h:42
bool operator<(const const_iterator &o) const
Definition: Selection.h:38
const value_type & operator*() const
Definition: Selection.h:41
const_iterator & operator=(const const_iterator &it)
Definition: Selection.h:30
const_iterator end() const
Definition: Selection.h:49