test_bifla.cxx
Go to the documentation of this file.
1 // boost iterator facade look-ahead test
2 
4 
5 #include <iostream>
6 using namespace std;
7 using namespace WireCell;
8 
9 
10 template<typename Container>
11 struct EveryOther {
12  typedef typename Container::value_type value_type;
13  typedef typename Container::iterator iterator;
15 
16  const_iterator it, other, end;
17 
18  EveryOther(const_iterator begin, const_iterator end)
19  : it(begin), other(begin), end(end) {
20  if (other != end) ++other;
21  if (other != end) ++other;
22  cerr << "Creating EveryOther " << std::distance(begin,end) << endl;
23  }
24  value_type operator()() {
25  value_type ret{};
26  if (*this) {
27  ret = *it + *other;
28  ++it;
29  ++other;
30  }
31  cerr << "operator() returns at " << std::distance(it,end) << endl;
32  return ret;
33  }
34  operator bool() const {
35  bool exhausted = false;
36  if (end-it < 2) {
37  exhausted = true;
38  }
39  cerr << "Are we exhausted? " << exhausted << endl;
40  return !exhausted;
41  }
42  bool operator==(const EveryOther& rhs) const {
43  if (this == &rhs) return true;
44  return it == rhs.it;
45  }
46 
47 };
48 
49 #include <vector>
50 
51 
52 typedef std::vector<int> IntVector;
55 typedef std::pair<eo_iterator, eo_iterator> eo_range;
56 
58 {
59  return eo_range(eo_iterator(EOIV(begin, end)),
60  eo_iterator(EOIV(end, end)));
61 }
62 
63 int main()
64 {
65  IntVector vi;
66  vi.push_back(0);
67  vi.push_back(1);
68  vi.push_back(2);
69  vi.push_back(3);
70  vi.push_back(4);
71  vi.push_back(5);
72  vi.push_back(6);
73 
74 
75  auto its = make_every_other(vi.begin(), vi.end());
76 
77  for (auto it = its.first; it != its.second; ++it) {
78  cout << *it << endl;
79  }
80  return 0;
81 }
intermediate_table::iterator iterator
eo_range make_every_other(IntVector::iterator begin, IntVector::iterator end)
Definition: test_bifla.cxx:57
STL namespace.
intermediate_table::const_iterator const_iterator
int main()
Definition: test_bifla.cxx:63
GeneratorIter< EOIV, int > eo_iterator
Definition: test_bifla.cxx:54
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:72
Container::value_type value_type
Definition: test_bifla.cxx:12
EveryOther< IntVector > EOIV
Definition: test_bifla.cxx:53
std::vector< int > IntVector
Definition: test_bifla.cxx:52
EveryOther(const_iterator begin, const_iterator end)
Definition: test_bifla.cxx:18
Container::const_iterator const_iterator
Definition: test_bifla.cxx:14
bool operator==(const EveryOther &rhs) const
Definition: test_bifla.cxx:42
double distance(double x1, double y1, double z1, double x2, double y2, double z2)
std::vector< int > IntVector
Definition: fcldump.cxx:27
Definition: Main.h:22
value_type operator()()
Definition: test_bifla.cxx:24
std::pair< eo_iterator, eo_iterator > eo_range
Definition: test_bifla.cxx:55
const_iterator it
Definition: test_bifla.cxx:16
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:67
int bool
Definition: qglobal.h:345
const_iterator other
Definition: test_bifla.cxx:16
Container::iterator iterator
Definition: test_bifla.cxx:13
QTextStream & endl(QTextStream &s)