CASM  1.1.0
A Clusters Approach to Statistical Mechanics
FilterIterator.hh
Go to the documentation of this file.
1 #ifndef FilteredConfigIterator_HH
2 #define FilteredConfigIterator_HH
3 
4 #include <iterator>
5 
7 
8 namespace CASM {
9 
10 template <typename IteratorType>
11 class FilterIterator;
12 
13 template <typename IteratorType>
14 void swap(FilterIterator<IteratorType> &a, FilterIterator<IteratorType> &b);
15 
19 template <typename IteratorType>
20 class FilterIterator : std::iterator<std::input_iterator_tag,
21  typename IteratorType::value_type> {
22  public:
23  using pointer = typename IteratorType::pointer;
24  using reference = typename IteratorType::reference;
25  using DataObject = typename IteratorType::value_type;
26 
28 
29  FilterIterator(IteratorType const &_begin, IteratorType const &_end,
30  std::function<bool(reference)> _filter)
31  : m_it(_begin), m_end(_end) {
32  while (m_it != m_end && !m_filter(*m_it)) {
33  ++m_it;
34  }
35  }
36 
37  FilterIterator(IteratorType const &_end) : m_it(_end), m_end(_end) {}
38 
39  reference operator*() const { return *m_it; }
40 
41  pointer operator->() const { return m_it.operator->(); }
42 
43  bool operator==(const FilterIterator &RHS) const { return m_it == RHS.m_it; }
44 
45  bool operator!=(const FilterIterator &RHS) const { return m_it != RHS.m_it; }
46 
48  ++m_it;
49  while (m_it != m_end && !m_filter(*m_it)) {
50  ++m_it;
51  }
52  return *this;
53  }
54 
56  FilterIterator<IteratorType> t_it(*this);
57  operator++();
58  return t_it;
59  }
60 
61  friend void swap<>(FilterIterator &a, FilterIterator &b);
62 
63  private:
64  IteratorType m_it, m_end;
65  std::function<bool(reference)> m_filter;
66 };
67 
69 template <typename IteratorType>
71  using std::swap;
72 
73  swap(a.m_it, b.m_it);
74  swap(a.m_end, b.m_end);
75  swap(a.m_filter, b.m_filter);
76 }
77 
78 template <typename IteratorType, typename DataObject>
80  IteratorType const &it, IteratorType const &it_end,
81  std::function<bool(typename FilterIterator<IteratorType>::reference)>
82  filter_function) {
83  return FilterIterator<IteratorType>(it, it_end, filter_function);
84 }
85 
86 template <typename IteratorType>
87 FilterIterator<IteratorType> filter_end(IteratorType const &it_end) {
88  return FilterIterator<IteratorType>(it_end);
89 }
90 
91 } // namespace CASM
92 #endif
typename IteratorType::pointer pointer
reference operator*() const
pointer operator->() const
std::function< bool(reference)> m_filter
typename IteratorType::reference reference
FilterIterator(IteratorType const &_begin, IteratorType const &_end, std::function< bool(reference)> _filter)
FilterIterator operator++(int)
FilterIterator & operator++()
typename IteratorType::value_type DataObject
bool operator!=(const FilterIterator &RHS) const
FilterIterator(IteratorType const &_end)
bool operator==(const FilterIterator &RHS) const
Main CASM namespace.
Definition: APICommand.hh:8
void swap(FilterIterator< IteratorType > &a, FilterIterator< IteratorType > &b)
Definitions.
void swap(ConfigDoF &A, ConfigDoF &B)
Definition: ConfigDoF.cc:260
FilteredConfigIterator< IteratorType > filter_begin(const IteratorType &it, const IteratorType &it_end, const std::vector< std::string > &filter_expr, const DataFormatterDictionary< DataObject > &_dict)
FilteredConfigIterator< IteratorType > filter_end(const IteratorType &it_end)