Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Iterators.hh
Go to the documentation of this file.
1 ///
2 /// @file Iterators.hh
3 /// @brief Iterator to cycle over outer products of containers
4 ///
5 
6 //**** This file is a part of the HAMMER library
7 //**** Copyright (C) 2016 - 2020 The HAMMER Collaboration
8 //**** HAMMER is licensed under version 3 of the GPL; see COPYING for details
9 //**** Please note the MCnet academic guidelines; see GUIDELINES for details
10 
11 // -*- C++ -*-
12 #ifndef HAMMER_TOOLS_ITERATORS
13 #define HAMMER_TOOLS_ITERATORS
14 
15 #include <utility>
16 #include <iterator>
17 #include <type_traits>
18 
19 namespace Hammer {
20 
21  template <typename OuterIterator, typename InnerIterator>
23  public:
24 
25  using outer_iterator = OuterIterator;
26  using inner_iterator = InnerIterator;
27 
28  using outer_category = typename std::iterator_traits<outer_iterator>::iterator_category;
29  using inner_category = typename std::iterator_traits<inner_iterator>::iterator_category;
30  using common_category = typename std::common_type<outer_category, inner_category>::type;
31 
32  using iterator_category =
33  typename std::conditional<std::is_same<common_category, std::random_access_iterator_tag>::value,
34  std::bidirectional_iterator_tag, common_category>::type;
35 
36  using inner_value_type = typename std::iterator_traits<inner_iterator>::value_type;
37  using outer_value_type = typename std::iterator_traits<outer_iterator>::value_type;
38  using value_type = typename std::pair<outer_value_type, outer_value_type>;
39  // using difference_type = typename std::iterator_traits<inner_iterator>::difference_type;
40  using inner_reference = typename std::iterator_traits<inner_iterator>::reference;
41  using outer_reference = typename std::iterator_traits<outer_iterator>::reference;
42  using inner_pointer = typename std::iterator_traits<inner_iterator>::pointer;
43  using outer_pointer = typename std::iterator_traits<outer_iterator>::pointer;
44  using pointer = typename std::pair<outer_pointer, inner_pointer>;
45  using reference = typename std::pair<outer_reference, inner_reference>;
46 
49  : outer_it_(out_it),
50  inner_it_(in_it),
51  outer_begin_(out_begin),
52  outer_end_(out_end),
53  inner_begin_(in_begin),
54  inner_end_(in_end) {
55  if (outer_begin_ == outer_end_) { return; }
56  if (inner_begin_ == inner_end_) { return; }
57 
58  if (outer_it_ == outer_end_) { return; }
59  if (inner_it_ == outer_end_) { return; }
60 
61  }
62 
64  : outer_it_(out_begin),
65  inner_it_(in_begin),
66  outer_begin_(out_begin),
67  outer_end_(out_end),
68  inner_begin_(in_begin),
69  inner_end_(in_end) {
70  if (outer_begin_ == outer_end_) { return; }
71  if (inner_begin_ == inner_end_) { return; }
72 
73  if (outer_it_ == outer_end_) { return; }
74  if (inner_it_ == outer_end_) { return; }
75 
76  }
77 
78  reference operator*() const { return make_pair(*outer_it_,*inner_it_); }
79  pointer operator->() const { return make_pair(&*outer_it_,&*inner_it_); }
80 
82  ++inner_it_;
83  if (inner_it_ == inner_end_) {
85  ++outer_it_;
86  }
87 
88  return *this;
89  }
90 
92  product_iterator it(*this);
93  ++*this;
94  return it;
95  }
96 
98  if(outer_it_ == outer_end_) {
99  --outer_it_;
101  }
102  if(inner_it_ == inner_begin_) {
103  if(outer_it_ == outer_begin_) {
104  return *this;
105  }
107  --outer_it_;
108  }
109  --inner_it_;
110 
111  return *this;
112  }
113 
115  product_iterator it(*this);
116  --*this;
117  return it;
118  }
119 
120  friend bool operator==(const product_iterator& a,
121  const product_iterator& b) {
122  return (a.outer_it_ == b.outer_it_) && (a.inner_it_ == b.inner_it_);
123  }
124 
125  friend bool operator!=(const product_iterator& a,
126  const product_iterator& b) {
127  return !(a == b);
128  }
129 
130  private:
131 
138  };
139 
140 }
141 
142 #endif
typename std::common_type< outer_category, inner_category >::type common_category
Definition: Iterators.hh:30
inner_iterator inner_begin_
Definition: Iterators.hh:136
product_iterator(outer_iterator out_begin, outer_iterator out_end, inner_iterator in_begin, inner_iterator in_end)
Definition: Iterators.hh:63
typename std::iterator_traits< inner_iterator >::iterator_category inner_category
Definition: Iterators.hh:29
typename std::iterator_traits< inner_iterator >::reference inner_reference
Definition: Iterators.hh:40
outer_iterator outer_end_
Definition: Iterators.hh:134
typename std::pair< outer_value_type, outer_value_type > value_type
Definition: Iterators.hh:38
OuterIterator outer_iterator
Definition: Iterators.hh:25
typename std::iterator_traits< outer_iterator >::value_type outer_value_type
Definition: Iterators.hh:37
product_iterator & operator--()
Definition: Iterators.hh:97
inner_iterator inner_end_
Definition: Iterators.hh:137
outer_iterator outer_it_
Definition: Iterators.hh:132
product_iterator(outer_iterator out_it, outer_iterator out_begin, outer_iterator out_end, inner_iterator in_it, inner_iterator in_begin, inner_iterator in_end)
Definition: Iterators.hh:48
typename std::iterator_traits< outer_iterator >::reference outer_reference
Definition: Iterators.hh:41
outer_iterator outer_begin_
Definition: Iterators.hh:133
typename std::iterator_traits< outer_iterator >::iterator_category outer_category
Definition: Iterators.hh:28
InnerIterator inner_iterator
Definition: Iterators.hh:26
typename std::iterator_traits< outer_iterator >::pointer outer_pointer
Definition: Iterators.hh:43
typename std::pair< outer_reference, inner_reference > reference
Definition: Iterators.hh:45
inner_iterator inner_it_
Definition: Iterators.hh:135
product_iterator operator++(int)
Definition: Iterators.hh:91
product_iterator & operator++()
Definition: Iterators.hh:81
friend bool operator==(const product_iterator &a, const product_iterator &b)
Definition: Iterators.hh:120
typename std::iterator_traits< inner_iterator >::value_type inner_value_type
Definition: Iterators.hh:36
product_iterator operator--(int)
Definition: Iterators.hh:114
pointer operator->() const
Definition: Iterators.hh:79
friend bool operator!=(const product_iterator &a, const product_iterator &b)
Definition: Iterators.hh:125
typename std::pair< outer_pointer, inner_pointer > pointer
Definition: Iterators.hh:44
typename std::iterator_traits< inner_iterator >::pointer inner_pointer
Definition: Iterators.hh:42
typename std::conditional< std::is_same< common_category, std::random_access_iterator_tag >::value, std::bidirectional_iterator_tag, common_category >::type iterator_category
Definition: Iterators.hh:34
reference operator*() const
Definition: Iterators.hh:78