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 
22  ////////////////////////////
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  {}
44 
45  reference operator*() const;
46  pointer operator->() const;
47  const_AssnsIter<L, R, D, Dir>& operator++();
48  const_AssnsIter<L, R, D, Dir> operator++(int);
49  const_AssnsIter<L, R, D, Dir>& operator--();
50  const_AssnsIter<L, R, D, Dir> operator--(int);
51  bool operator==(art::const_AssnsIter<L, R, D, Dir> const& iter) const;
52  bool operator!=(art::const_AssnsIter<L, R, D, Dir> const& iter) const;
54  const_AssnsIter<L, R, D, Dir> operator+(std::size_t i) const;
55  const_AssnsIter<L, R, D, Dir>& operator-=(std::size_t i);
56  const_AssnsIter<L, R, D, Dir> operator-(std::size_t i) const;
57  std::size_t operator-(
58  art::const_AssnsIter<L, R, D, Dir> const& iter1) const;
59  value_type operator[](std::size_t i) const;
60 
61  bool operator<(art::const_AssnsIter<L, R, D, Dir> const& iter) const;
62  bool operator<=(art::const_AssnsIter<L, R, D, Dir> const& iter) const;
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 
66  std::size_t
67  getIndex() const
68  {
69  return index_;
70  };
71 
72  private:
73  art::Assns<L, R, D> const* coll_{nullptr};
74  std::size_t index_{-1ull};
75  mutable AssnsNode<L, R, D> node_{};
76  };
77 
78  // For reverse iterators, we do not shift the underlying index into
79  // the collection since this wreaks havoc with the comparison
80  // operators. The shifting happens during dereferencing. Note that
81  // an attempt to dereference rend() will result in an out-of-range
82  // error.
83  template <Direction Dir>
84  constexpr auto
85  index_for_dereferencing(std::size_t const i)
86  {
87  return (Dir == Direction::Forward) ? i : i - 1;
88  }
89 
90  // Utilities for determining the left and right operands of iterator
91  // comparisons based on the direction of the iterator.
92  template <typename L, typename R, typename D, Direction Dir>
93  constexpr auto const&
96  {
97  return (Dir == Direction::Forward) ? a : b;
98  }
99 
100  template <typename L, typename R, typename D, Direction Dir>
101  constexpr auto const&
104  {
105  return (Dir == Direction::Forward) ? b : a;
106  }
107 
108  // Dereference
109  template <typename L, typename R, typename D, Direction Dir>
112  {
113  auto const index = index_for_dereferencing<Dir>(index_);
114  node_.first = (*coll_)[index].first;
115  node_.second = (*coll_)[index].second;
116  node_.data = &(coll_->data(index));
117  return node_;
118  }
119 
120  // right arrow
121  template <typename L, typename R, typename D, Direction Dir>
124  {
125  auto const index = index_for_dereferencing<Dir>(index_);
126  node_.first = (*coll_)[index].first;
127  node_.second = (*coll_)[index].second;
128  node_.data = &(coll_->data(index));
129  return &node_;
130  }
131 
132  // Pre-increment
133  template <typename L, typename R, typename D, Direction Dir>
136  {
137  index_ = index_ + signed_one<Dir>();
138  return *this;
139  }
140 
141  // Post-increment
142  template <typename L, typename R, typename D, Direction Dir>
145  {
147  index_ = index_ + signed_one<Dir>();
148  return tmp;
149  }
150 
151  // Pre-decrement
152  template <typename L, typename R, typename D, Direction Dir>
155  {
156  index_ = index_ - signed_one<Dir>();
157  return *this;
158  }
159  // post-decrement
160  template <typename L, typename R, typename D, Direction Dir>
163  {
165  index_ = index_ - signed_one<Dir>();
166  return tmp;
167  }
168 
169  // equality
170  template <typename L, typename R, typename D, Direction Dir>
171  bool
173  art::const_AssnsIter<L, R, D, Dir> const& iter) const
174  {
175  return index_ == iter.index_;
176  }
177 
178  // in-equality
179  template <typename L, typename R, typename D, Direction Dir>
180  bool
182  art::const_AssnsIter<L, R, D, Dir> const& iter) const
183  {
184  return !(index_ == iter.index_);
185  }
186 
187  // increment by a given value ...
188  template <typename L, typename R, typename D, Direction Dir>
191  {
192  // to do add check for index bounds and make sure it works for both positive
193  // and negative values
194  index_ = index_ + signed_one<Dir>() * i;
195  return *this;
196  }
197 
198  // random access
199  template <typename L, typename R, typename D, Direction Dir>
201  const_AssnsIter<L, R, D, Dir>::operator+(std::size_t const i) const
202  {
204  tmp.index_ = tmp.index_ + signed_one<Dir>() * i;
205  return tmp;
206  }
207 
208  // decrement by a given value ...
209  template <typename L, typename R, typename D, Direction Dir>
212  {
213  // to do add check for index bounds and make sure it works for both positive
214  // and negative values
215  index_ = index_ - signed_one<Dir>() * i;
216  return *this;
217  }
218 
219  // random access
220  template <typename L, typename R, typename D, Direction Dir>
222  const_AssnsIter<L, R, D, Dir>::operator-(std::size_t const i) const
223  {
225  tmp.index_ = tmp.index_ - signed_one<Dir>() * i;
226  return tmp;
227  }
228 
229  // difference between two iterators to return an index
230  template <typename L, typename R, typename D, Direction Dir>
231  std::size_t
233  art::const_AssnsIter<L, R, D, Dir> const& iter1) const
234  {
235  return (iter1.index_ - index_);
236  }
237 
238  // Dereference
239  template <typename L, typename R, typename D, Direction Dir>
241  const_AssnsIter<L, R, D, Dir>::operator[](std::size_t const i) const
242  {
244  tmp.index_ = tmp.index_ + signed_one<Dir>() * i;
245  return tmp.node_;
246  }
247 
248  // less than
249  template <typename L, typename R, typename D, Direction Dir>
250  bool
252  art::const_AssnsIter<L, R, D, Dir> const& iter) const
253  {
254  auto const& l = left(*this, iter);
255  auto const& r = right(*this, iter);
256  return l.index_ < r.index_;
257  }
258 
259  // less than equal to
260  template <typename L, typename R, typename D, Direction Dir>
261  bool
263  art::const_AssnsIter<L, R, D, Dir> const& iter) const
264  {
265  auto const& l = left(*this, iter);
266  auto const& r = right(*this, iter);
267  return l.index_ <= r.index_;
268  }
269 
270  // less than equal to
271  template <typename L, typename R, typename D, Direction Dir>
272  bool
274  art::const_AssnsIter<L, R, D, Dir> const& iter) const
275  {
276  auto const& l = left(*this, iter);
277  auto const& r = right(*this, iter);
278  return l.index_ > r.index_;
279  }
280 
281  // greater than equal to
282  template <typename L, typename R, typename D, Direction Dir>
283  bool
285  art::const_AssnsIter<L, R, D, Dir> const& iter) const
286  {
287  auto const& l = left(*this, iter);
288  auto const& r = right(*this, iter);
289  return l.index_ >= r.index_;
290  }
291 }
292 
293 #endif /* canvas_Persistency_Common_AssnsIter_h */
294 
295 // Local Variables:
296 // mode: c++
297 // End:
reference operator*() const
Definition: AssnsIter.h:111
const_AssnsIter< L, R, D, Dir > & operator+=(std::size_t i)
Definition: AssnsIter.h:190
value_type const & reference
Definition: AssnsIter.h:33
bool operator==(Provenance const &a, Provenance const &b) noexcept
Definition: Provenance.cc:141
pointer operator->() const
Definition: AssnsIter.h:123
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:102
std::size_t getIndex() const
Definition: AssnsIter.h:67
bool operator>(ScheduleID const left, ScheduleID const right) noexcept
Definition: ScheduleID.cc:53
bool operator>=(art::const_AssnsIter< L, R, D, Dir > const &iter) const
Definition: AssnsIter.h:284
DoubleProduct & operator+=(DoubleProduct &left, DoubleProduct const &right)
Definition: ToyProducts.h:103
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:273
value_type const * pointer
Definition: AssnsIter.h:32
size_type size() const
Definition: Assns.h:497
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:251
#define D
Debug message.
Definition: tclscanner.cpp:775
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
static QStrList * l
Definition: config.cpp:1044
const_AssnsIter< L, R, D, Dir > & operator--()
Definition: AssnsIter.h:154
bool operator<=(art::const_AssnsIter< L, R, D, Dir > const &iter) const
Definition: AssnsIter.h:262
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:201
const double a
const_AssnsIter< L, R, D, Dir > & operator++()
Definition: AssnsIter.h:135
string tmp
Definition: languages.py:63
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:94
DoubleProduct operator+(DoubleProduct const &left, DoubleProduct const right)
Definition: ToyProducts.h:97
constexpr int signed_one()
Definition: AssnsIter.h:17
std::size_t index_
Definition: AssnsIter.h:74
constexpr auto index_for_dereferencing(std::size_t const i)
Definition: AssnsIter.h:85
value_type operator[](std::size_t i) const
Definition: AssnsIter.h:241
std::ptrdiff_t difference_type
Definition: AssnsIter.h:34
bool operator==(art::const_AssnsIter< L, R, D, Dir > const &iter) const
Definition: AssnsIter.h:172
const_AssnsIter< L, R, D, Dir > operator-(std::size_t i) const
Definition: AssnsIter.h:222
static bool * b
Definition: config.cpp:1043
Direction
Definition: AssnsIter.h:13
const_AssnsIter< L, R, D, Dir > & operator-=(std::size_t i)
Definition: AssnsIter.h:211
bool operator>=(ScheduleID const left, ScheduleID const right) noexcept
Definition: ScheduleID.cc:59
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:181