Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:08:22

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 <cstddef>
0012 #include <iterator>
0013 
0014 namespace Acts {
0015 
0016 template <typename Index>
0017 class IndexRange {
0018  public:
0019   using index_type = Index;
0020   using index_range_type = std::pair<index_type, index_type>;
0021 
0022   class Iterator {
0023    public:
0024     using value_type = Index;
0025     using difference_type = std::ptrdiff_t;
0026     using pointer = Index *;
0027     using reference = Index &;
0028 
0029     using iterator_category = std::random_access_iterator_tag;
0030     using iterator_concept = std::random_access_iterator_tag;
0031 
0032     constexpr Iterator() noexcept = default;
0033     explicit constexpr Iterator(Index index) noexcept : m_index{index} {}
0034 
0035     constexpr value_type operator*() const noexcept { return m_index; }
0036     constexpr value_type operator[](difference_type n) const noexcept {
0037       return m_index + n;
0038     }
0039 
0040     constexpr Iterator &operator++() noexcept {
0041       ++m_index;
0042       return *this;
0043     }
0044     constexpr Iterator operator++(int) noexcept {
0045       auto tmp = *this;
0046       ++(*this);
0047       return tmp;
0048     }
0049     constexpr Iterator &operator--() noexcept {
0050       --m_index;
0051       return *this;
0052     }
0053     constexpr Iterator operator--(int) noexcept {
0054       auto tmp = *this;
0055       --(*this);
0056       return tmp;
0057     }
0058 
0059     constexpr Iterator &operator+=(difference_type n) noexcept {
0060       m_index += n;
0061       return *this;
0062     }
0063     constexpr Iterator &operator-=(difference_type n) noexcept {
0064       m_index -= n;
0065       return *this;
0066     }
0067 
0068    private:
0069     Index m_index{0};
0070 
0071     friend constexpr Iterator operator+(Iterator it,
0072                                         difference_type n) noexcept {
0073       return it += n;
0074     }
0075 
0076     friend constexpr Iterator operator+(difference_type n,
0077                                         Iterator it) noexcept {
0078       return it += n;
0079     }
0080 
0081     friend constexpr Iterator operator-(Iterator it,
0082                                         difference_type n) noexcept {
0083       return it -= n;
0084     }
0085 
0086     friend constexpr difference_type operator-(const Iterator &lhs,
0087                                                const Iterator &rhs) noexcept {
0088       return lhs.m_index - rhs.m_index;
0089     }
0090 
0091     friend constexpr auto operator<=>(const Iterator &a,
0092                                       const Iterator &b) noexcept = default;
0093     friend constexpr bool operator==(const Iterator &a,
0094                                      const Iterator &b) noexcept = default;
0095   };
0096   using iterator = Iterator;
0097 
0098   explicit IndexRange(index_range_type range) noexcept : m_range(range) {}
0099 
0100   std::size_t size() const noexcept { return m_range.second - m_range.first; }
0101   bool empty() const noexcept { return size() == 0; }
0102 
0103   iterator begin() const noexcept { return iterator(m_range.first); }
0104   iterator end() const noexcept { return iterator(m_range.second); }
0105 
0106  private:
0107   index_range_type m_range{};
0108 };
0109 
0110 }  // namespace Acts