LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
counter.h
Go to the documentation of this file.
1 
10 #ifndef LARCOREALG_COREUTILS_COUNTER_H
11 #define LARCOREALG_COREUTILS_COUNTER_H
12 
13 // LArSoft libraries
15 
16 // C/C++ libraries
17 #include <cstddef> // std::size_t
18 #include <utility> // std::move()
19 
20 namespace util {
21 
22  // -- BEGIN -- Counted iterations --------------------------------------------
25 
51  template <typename T = std::size_t>
53 
54  public:
55  // --- BEGIN -- Traits and data types --------------------------------------
58 
60 
61  using difference_type = std::ptrdiff_t;
62  using value_type = T;
63  using reference = T const&;
64  using pointer = T*;
65  using iterator_category = std::bidirectional_iterator_tag;
66 
68  // --- END -- Traits and data types ----------------------------------------
69 
70  // --- BEGIN Constructors --------------------------------------------------
73 
80  count_iterator() = default;
81 
86  count_iterator(value_type count) : fCount(count) {}
87 
89  // --- END Constructors ----------------------------------------------------
90 
91  // --- BEGIN -- Data access ------------------------------------------------
94 
96  reference operator*() const { return fCount; }
97 
99  // --- END -- Data access --------------------------------------------------
100 
101  // --- BEGIN -- Modification -----------------------------------------------
104 
107  {
108  ++fCount;
109  return *this;
110  }
111 
115  {
116  iterator_type const old = *this;
117  operator++();
118  return old;
119  }
120 
123  {
124  --fCount;
125  return *this;
126  }
127 
131  {
132  iterator_type const old = *this;
133  operator--();
134  return old;
135  }
136 
138  // --- END -- Modification -------------------------------------------------
139 
140  // --- BEGIN -- Comparisons ------------------------------------------------
143 
145  template <typename U>
146  bool operator==(count_iterator<U> const& other) const
147  {
148  return fCount == other.fCount;
149  }
150 
152  template <typename U>
153  bool operator!=(count_iterator<U> const& other) const
154  {
155  return fCount != other.fCount;
156  }
157 
159  // --- END -- Comparisons --------------------------------------------------
160 
161  private:
163 
164  }; // class count_iterator<>
165 
188  template <typename T>
189  auto counter(T begin, T end);
190 
193  template <typename T>
194  auto counter(T end)
195  {
196  return counter(T{}, end);
197  }
198 
218  template <typename T = std::size_t>
219  auto infinite_counter(T begin = T{});
220 
222  // -- END -- Counted iterations ----------------------------------------------
223 
224 } // namespace util
225 
226 //==============================================================================
227 //=== template implementation
228 //==============================================================================
229 //------------------------------------------------------------------------------
230 //--- util::counter()
231 //------------------------------------------------------------------------------
232 namespace util::details {
233 
243  template <typename T>
247 
248  public:
249  // mock-up of stuff required by `util::span`
250  using value_type = T;
251  using reference = T const&;
252  using pointer = T const*;
253  using difference_type = std::ptrdiff_t;
254  using iterator_category = std::forward_iterator_tag; // this is a lie
255 
257  bool operator==(this_iterator_t const&) const { return true; }
258 
260  bool operator!=(this_iterator_t const&) const { return false; }
261 
262  }; // class infinite_endcount_iterator
263 
265  template <typename T>
267  {
268  return true;
269  }
270 
271  template <typename T>
273  {
274  return true;
275  }
276 
277  template <typename T>
279  {
280  return false;
281  }
282 
283  template <typename T>
285  {
286  return false;
287  }
288 
289  //----------------------------------------------------------------------------
290 
291 } // namespace util::details
292 
293 //------------------------------------------------------------------------------
294 template <typename T>
296 {
297  return util::span(count_iterator(begin), count_iterator(end));
298 }
299 
300 //------------------------------------------------------------------------------
301 template <typename T>
303 {
305 } // util::infinite_counter()
306 
307 //------------------------------------------------------------------------------
308 
309 #endif // LARCOREALG_COREUTILS_COUNTER_H
T value_type
Type of index returned by this iterator.
Definition: counter.h:62
Namespace for general, non-LArSoft-specific utilities.
Definition: PIDAAlg.h:26
An object with a begin and end iterator.
An iterator dereferencing to a counter value.
Definition: counter.h:52
auto infinite_counter(T begin=T{})
Version of util::counter() starting at begin and never ending.
Definition: counter.h:302
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:77
std::ptrdiff_t difference_type
Type of this iterator.
Definition: counter.h:61
iterator_type operator--(int) const
Definition: counter.h:130
auto counter(T begin, T end)
Returns an object to iterate values from begin to end in a range-for loop.
Definition: counter.h:295
Class used as end iterator (sentinel) for an infinite loop.
Definition: counter.h:244
T const & reference
Type returned by dereference operator.
Definition: counter.h:63
bool operator!=(this_iterator_t const &) const
Never admit this iterator is equal to anything else (except the same).
Definition: counter.h:260
bool operator==(this_iterator_t const &) const
Never admit this iterator is equal to anything else (except the same).
Definition: counter.h:257
count_iterator(value_type count)
Initializes the iterator with the specified loop count.
Definition: counter.h:86
reference operator*() const
Returns the current loop count.
Definition: counter.h:96
T * pointer
Type of this iterator.
Definition: counter.h:64
value_type fCount
Internal counter.
Definition: counter.h:162
iterator_type & operator++()
Increments the loop count of this iterator, which is then returned.
Definition: counter.h:106
span(IterB &&b, IterE &&e, Adaptor &&adaptor) -> span< decltype(adaptor(std::forward< IterB >(b))), decltype(adaptor(std::forward< IterE >(e))) >
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:69
iterator_type & operator--()
Decrements the loop count of this iterator, which is then returned.
Definition: counter.h:122
std::forward_iterator_tag iterator_category
Definition: counter.h:254
bool operator!=(count_iterator< U > const &other) const
Iterators are equal if their loop counts compare different.
Definition: counter.h:153
count_iterator()=default
Initializes the iterator.
iterator_type operator++(int) const
Definition: counter.h:114
bool operator==(count_iterator< U > const &other) const
Iterators are equal if their loop counts compare equal.
Definition: counter.h:146
std::bidirectional_iterator_tag iterator_category
Type of this iterator.
Definition: counter.h:65