LArSoft  v08_20_00
Liquid Argon Software toolkit - http://larsoft.org/
AssnsIter.h
Go to the documentation of this file.
1 #ifndef canvas_Persistency_Common_AssnsIter_h
2 #define canvas_Persistency_Common_AssnsIter_h
3 
4 /* Assns Iterator for art::Assns<L, R, D> */
5 
8 
9 #include <iostream>
10 #include <iterator>
11 
12 namespace art {
13  enum class Direction : int { Forward = 1, Reverse = -1 };
14 
15  template <Direction Dir>
16  constexpr int
18  {
19  return static_cast<std::underlying_type_t<Direction>>(Dir);
20  }
21 
23  // Const Iterator
24  template <typename L,
25  typename R,
26  typename D,
29  public:
30  using iterator_category = std::random_access_iterator_tag;
32  using pointer = value_type const*;
33  using reference = value_type const&;
34  using difference_type = std::ptrdiff_t;
35 
36  const_AssnsIter() = default;
37  explicit const_AssnsIter(art::Assns<L, R, D> const& assns)
38  : coll_{&assns}, index_{assns.size()}
39  {}
40  explicit const_AssnsIter(art::Assns<L, R, D> const& assns,
41  std::size_t const i)
42  : coll_{&assns}, index_{i}
43  {}
46 
47  reference operator*() const;
48  pointer operator->() const;
49  const_AssnsIter<L, R, D, Dir>& operator++();
50  const_AssnsIter<L, R, D, Dir> operator++(int);
51  const_AssnsIter<L, R, D, Dir>& operator--();
52  const_AssnsIter<L, R, D, Dir> operator--(int);
53  bool operator==(art::const_AssnsIter<L, R, D, Dir> const& iter) const;
54  bool operator!=(art::const_AssnsIter<L, R, D, Dir> const& iter) const;
56  const_AssnsIter<L, R, D, Dir> operator+(std::size_t i) const;
57  const_AssnsIter<L, R, D, Dir>& operator-=(std::size_t i);
58  const_AssnsIter<L, R, D, Dir> operator-(std::size_t i) const;
59  std::size_t operator-(
60  art::const_AssnsIter<L, R, D, Dir> const& iter1) const;
61  value_type operator[](std::size_t i) const;
62 
63  bool operator<(art::const_AssnsIter<L, R, D, Dir> const& iter) const;
64  bool operator<=(art::const_AssnsIter<L, R, D, Dir> const& iter) const;
65  bool operator>(art::const_AssnsIter<L, R, D, Dir> const& iter) const;
66  bool operator>=(art::const_AssnsIter<L, R, D, Dir> const& iter) const;
67 
68  std::size_t
69  getIndex() const
70  {
71  return index_;
72  };
73 
74  private:
75  art::Assns<L, R, D> const* coll_{nullptr};
76  std::size_t index_{-1ull};
77  mutable AssnsNode<L, R, D> node_{};
78  };
79 
80  // For reverse iterators, we do not shift the underlying index into
81  // the collection since this wreaks havoc with the comparison
82  // operators. The shifting happens during dereferencing. Note that
83  // an attempt to dereference rend() will result in an out-of-range
84  // error.
85  template <Direction Dir>
86  constexpr auto
87  index_for_dereferencing(std::size_t const i)
88  {
89  return (Dir == Direction::Forward) ? i : i - 1;
90  }
91 
92  // Utilities for determining the left and right operands of iterator
93  // comparisons based on the direction of the iterator.
94  template <typename L, typename R, typename D, Direction Dir>
95  constexpr auto const&
98  {
99  return (Dir == Direction::Forward) ? a : b;
100  }
101 
102  template <typename L, typename R, typename D, Direction Dir>
103  constexpr auto const&
106  {
107  return (Dir == Direction::Forward) ? b : a;
108  }
109 
110  // Dereference
111  template <typename L, typename R, typename D, Direction Dir>
114  {
115  auto const index = index_for_dereferencing<Dir>(index_);
116  node_.first = (*coll_)[index].first;
117  node_.second = (*coll_)[index].second;
118  node_.data = &(coll_->data(index));
119  return node_;
120  }
121 
122  // right arrow
123  template <typename L, typename R, typename D, Direction Dir>
126  {
127  auto const index = index_for_dereferencing<Dir>(index_);
128  node_.first = (*coll_)[index].first;
129  node_.second = (*coll_)[index].second;
130  node_.data = &(coll_->data(index));
131  return &node_;
132  }
133 
134  // Pre-increment
135  template <typename L, typename R, typename D, Direction Dir>
138  {
139  index_ = index_ + signed_one<Dir>();
140  return *this;
141  }
142 
143  // Post-increment
144  template <typename L, typename R, typename D, Direction Dir>
147  {
149  index_ = index_ + signed_one<Dir>();
150  return tmp;
151  }
152 
153  // Pre-decrement
154  template <typename L, typename R, typename D, Direction Dir>
157  {
158  index_ = index_ - signed_one<Dir>();
159  return *this;
160  }
161  // post-decrement
162  template <typename L, typename R, typename D, Direction Dir>
165  {
167  index_ = index_ - signed_one<Dir>();
168  return tmp;
169  }
170 
171  // equality
172  template <typename L, typename R, typename D, Direction Dir>
173  bool
175  art::const_AssnsIter<L, R, D, Dir> const& iter) const
176  {
177  return index_ == iter.index_;
178  }
179 
180  // in-equality
181  template <typename L, typename R, typename D, Direction Dir>
182  bool
184  art::const_AssnsIter<L, R, D, Dir> const& iter) const
185  {
186  return !(index_ == iter.index_);
187  }
188 
189  // increment by a given value ...
190  template <typename L, typename R, typename D, Direction Dir>
193  {
194  // to do add check for index bounds and make sure it works for both positive
195  // and negative values
196  index_ = index_ + signed_one<Dir>() * i;
197  return *this;
198  }
199 
200  // random access
201  template <typename L, typename R, typename D, Direction Dir>
203  const_AssnsIter<L, R, D, Dir>::operator+(std::size_t const i) const
204  {
206  tmp.index_ = tmp.index_ + signed_one<Dir>() * i;
207  return tmp;
208  }
209 
210  // decrement by a given value ...
211  template <typename L, typename R, typename D, Direction Dir>
214  {
215  // to do add check for index bounds and make sure it works for both positive
216  // and negative values
217  index_ = index_ - signed_one<Dir>() * i;
218  return *this;
219  }
220 
221  // random access
222  template <typename L, typename R, typename D, Direction Dir>
224  const_AssnsIter<L, R, D, Dir>::operator-(std::size_t const i) const
225  {
227  tmp.index_ = tmp.index_ - signed_one<Dir>() * i;
228  return tmp;
229  }
230 
231  // difference between two iterators to return an index
232  template <typename L, typename R, typename D, Direction Dir>
233  std::size_t
235  art::const_AssnsIter<L, R, D, Dir> const& iter1) const
236  {
237  return (iter1.index_ - index_);
238  }
239 
240  // Dereference
241  template <typename L, typename R, typename D, Direction Dir>
243  const_AssnsIter<L, R, D, Dir>::operator[](std::size_t const i) const
244  {
246  tmp.index_ = tmp.index_ + signed_one<Dir>() * i;
247  return tmp.node_;
248  }
249 
250  // less than
251  template <typename L, typename R, typename D, Direction Dir>
252  bool
254  art::const_AssnsIter<L, R, D, Dir> const& iter) const
255  {
256  auto const& l = left(*this, iter);
257  auto const& r = right(*this, iter);
258  return l.index_ < r.index_;
259  }
260 
261  // less than equal to
262  template <typename L, typename R, typename D, Direction Dir>
263  bool
265  art::const_AssnsIter<L, R, D, Dir> const& iter) const
266  {
267  auto const& l = left(*this, iter);
268  auto const& r = right(*this, iter);
269  return l.index_ <= r.index_;
270  }
271 
272  // less than equal to
273  template <typename L, typename R, typename D, Direction Dir>
274  bool
276  art::const_AssnsIter<L, R, D, Dir> const& iter) const
277  {
278  auto const& l = left(*this, iter);
279  auto const& r = right(*this, iter);
280  return l.index_ > r.index_;
281  }
282 
283  // greater than equal to
284  template <typename L, typename R, typename D, Direction Dir>
285  bool
287  art::const_AssnsIter<L, R, D, Dir> const& iter) const
288  {
289  auto const& l = left(*this, iter);
290  auto const& r = right(*this, iter);
291  return l.index_ >= r.index_;
292  }
293 
294  template <typename L, typename R, typename D, Direction Dir>
298  {
299  node_ = iter.node_;
300  return *this;
301  }
302 }
303 
304 #endif /* canvas_Persistency_Common_AssnsIter_h */
305 
306 // Local Variables:
307 // mode: c++
308 // End:
reference operator*() const
Definition: AssnsIter.h:113
size_type size() const
Definition: Assns.h:447
const_AssnsIter< L, R, D, Dir > & operator+=(std::size_t i)
Definition: AssnsIter.h:192
value_type const & reference
Definition: AssnsIter.h:33
pointer operator->() const
Definition: AssnsIter.h:125
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:104
std::size_t getIndex() const
Definition: AssnsIter.h:69
bool operator>(ScheduleID const left, ScheduleID const right) noexcept
Definition: ScheduleID.cc:55
bool operator>=(art::const_AssnsIter< L, R, D, Dir > const &iter) const
Definition: AssnsIter.h:286
std::random_access_iterator_tag iterator_category
Definition: AssnsIter.h:30
bool operator>(art::const_AssnsIter< L, R, D, Dir > const &iter) const
Definition: AssnsIter.h:275
value_type const * pointer
Definition: AssnsIter.h:32
const_AssnsIter(art::Assns< L, R, D > const &assns)
Definition: AssnsIter.h:37
bool operator<(art::const_AssnsIter< L, R, D, Dir > const &iter) const
Definition: AssnsIter.h:253
Float_t tmp
Definition: plot.C:37
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &)
const_AssnsIter(art::Assns< L, R, D > const &assns, std::size_t const i)
Definition: AssnsIter.h:40
const_AssnsIter< L, R, D, Dir > & operator--()
Definition: AssnsIter.h:156
bool operator<=(art::const_AssnsIter< L, R, D, Dir > const &iter) const
Definition: AssnsIter.h:264
QuadExpr operator-(double v, const QuadExpr &e)
Definition: QuadExpr.h:38
const_AssnsIter< L, R, D, Dir > operator+(std::size_t i) const
Definition: AssnsIter.h:203
Double_t R
String & operator+=(String &s, VectorDumper< Vector > const &manip)
Appends a string rendering of a vector to the specified string.
Definition: DumpUtils.h:424
const_AssnsIter< L, R, D, Dir > & operator++()
Definition: AssnsIter.h:137
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:96
constexpr int signed_one()
Definition: AssnsIter.h:17
QuadExpr operator+(double v, const QuadExpr &e)
Definition: QuadExpr.h:37
std::size_t index_
Definition: AssnsIter.h:76
constexpr auto index_for_dereferencing(std::size_t const i)
Definition: AssnsIter.h:87
value_type operator[](std::size_t i) const
Definition: AssnsIter.h:243
std::ptrdiff_t difference_type
Definition: AssnsIter.h:34
bool operator==(art::const_AssnsIter< L, R, D, Dir > const &iter) const
Definition: AssnsIter.h:174
const_AssnsIter< L, R, D, Dir > operator-(std::size_t i) const
Definition: AssnsIter.h:224
Direction
Definition: AssnsIter.h:13
AssnsNode< L, R, D > node_
Definition: AssnsIter.h:77
const_AssnsIter< L, R, D, Dir > & operator-=(std::size_t i)
Definition: AssnsIter.h:213
bool operator>=(ScheduleID const left, ScheduleID const right) noexcept
Definition: ScheduleID.cc:61
bool operator==(Provenance const &a, Provenance const &b)
Definition: Provenance.cc:139
const_AssnsIter< L, R, D, Dir > & operator=(art::const_AssnsIter< L, R, D, Dir > const &iter)
Definition: AssnsIter.h:296
QuadExpr operator*(double v, const QuadExpr &e)
Definition: QuadExpr.h:39
bool operator!=(art::const_AssnsIter< L, R, D, Dir > const &iter) const
Definition: AssnsIter.h:183