Juggler
Juggling algorithms and event processing using gaudi framework
Range.hpp
Go to the documentation of this file.
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2019 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
11 #include <iterator>
12 #include <utility>
13 
14 namespace Jug {
15 
16 /// A wrapper around a pair of iterators to simplify range-based loops.
17 ///
18 /// Some standard library algorithms return pairs of iterators to identify
19 /// a sub-range. This wrapper simplifies the iteration and should be used as
20 /// follows:
21 ///
22 /// for (auto x : makeRange(std::equal_range(...)) {
23 /// ...
24 /// }
25 ///
26 template <typename Iterator>
27 class Range {
28  public:
29  Range(Iterator b, Iterator e) : m_begin(b), m_end(e) {}
30  Range(Range&&) = default;
31  Range(const Range&) = default;
32  ~Range() = default;
33  Range& operator=(Range&&) = default;
34  Range& operator=(const Range&) = default;
35 
36  Iterator begin() const { return m_begin; }
37  Iterator end() const { return m_end; }
38  bool empty() const { return m_begin == m_end; }
39  std::size_t size() const { return std::distance(m_begin, m_end); }
40 
41  private:
42  Iterator m_begin;
43  Iterator m_end;
44 };
45 
46 template <typename Iterator>
47 Range<Iterator> makeRange(Iterator begin, Iterator end) {
48  return Range<Iterator>(begin, end);
49 }
50 
51 template <typename Iterator>
52 Range<Iterator> makeRange(std::pair<Iterator, Iterator> range) {
53  return Range<Iterator>(range.first, range.second);
54 }
55 
56 } // namespace FW
Jug::Range::empty
bool empty() const
Definition: Range.hpp:38
Jug::Range::operator=
Range & operator=(const Range &)=default
Jug::Range::Range
Range(const Range &)=default
Jug
Definition: DD4hepBField.h:22
Jug::makeRange
Range< Iterator > makeRange(Iterator begin, Iterator end)
Definition: Range.hpp:47
Jug::Range::Range
Range(Range &&)=default
Jug::Range
Definition: Range.hpp:27
Jug::Range::end
Iterator end() const
Definition: Range.hpp:37
Jug::Range::Range
Range(Iterator b, Iterator e)
Definition: Range.hpp:29
Jug::Range::~Range
~Range()=default
Jug::Range::begin
Iterator begin() const
Definition: Range.hpp:36
Jug::Range::size
std::size_t size() const
Definition: Range.hpp:39
Jug::Range::operator=
Range & operator=(Range &&)=default