Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:11:47

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include <iterator>
0012 #include <utility>
0013 
0014 namespace ActsExamples {
0015 
0016 /// A wrapper around a pair of iterators to simplify range-based loops.
0017 ///
0018 /// Some standard library algorithms return pairs of iterators to identify
0019 /// a sub-range. This wrapper simplifies the iteration and should be used as
0020 /// follows:
0021 ///
0022 ///     for (auto x : makeRange(std::equal_range(...)) {
0023 ///         ...
0024 ///     }
0025 ///
0026 template <typename Iterator>
0027 class Range {
0028  public:
0029   Range(Iterator b, Iterator e) : m_begin(b), m_end(e) {}
0030   Range(Range&&) noexcept = default;
0031   Range(const Range&) = default;
0032   ~Range() = default;
0033   Range& operator=(Range&&) noexcept = default;
0034   Range& operator=(const Range&) = default;
0035 
0036   Iterator begin() const { return m_begin; }
0037   Iterator end() const { return m_end; }
0038   bool empty() const { return m_begin == m_end; }
0039   std::size_t size() const { return std::distance(m_begin, m_end); }
0040 
0041  private:
0042   Iterator m_begin;
0043   Iterator m_end;
0044 };
0045 
0046 template <typename Iterator>
0047 Range<Iterator> makeRange(Iterator begin, Iterator end) {
0048   return Range<Iterator>(begin, end);
0049 }
0050 
0051 template <typename Iterator>
0052 Range<Iterator> makeRange(std::pair<Iterator, Iterator> range) {
0053   return Range<Iterator>(range.first, range.second);
0054 }
0055 
0056 }  // namespace ActsExamples