Back to home page

EIC code displayed by LXR

 
 

    


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

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 "Acts/Utilities/TypeTraits.hpp"
0012 
0013 #include <utility>
0014 
0015 namespace Acts {
0016 
0017 template <typename Derived, typename DerivedReadOnly, typename Container,
0018           typename Index, bool ReadOnly>
0019 class ContainerRange {
0020  public:
0021   using container_type = const_if_t<ReadOnly, Container>;
0022   using index_type = Index;
0023   static constexpr bool read_only = ReadOnly;
0024   using index_range_type = std::pair<index_type, index_type>;
0025 
0026   constexpr ContainerRange(container_type &container,
0027                            const index_range_type &range) noexcept
0028       : m_container(&container), m_range(range) {}
0029   template <bool OtherReadOnly>
0030   explicit constexpr ContainerRange(
0031       const ContainerRange<Derived, DerivedReadOnly, Container, Index,
0032                            OtherReadOnly> &other) noexcept
0033     requires(ReadOnly && !OtherReadOnly)
0034       : m_container(&other.container()), m_range(other.range()) {}
0035 
0036   constexpr DerivedReadOnly asConst() const noexcept
0037     requires(!ReadOnly)
0038   {
0039     return {container(), range()};
0040   }
0041 
0042   constexpr container_type &container() const noexcept { return *m_container; }
0043   constexpr const index_range_type &range() const noexcept { return m_range; }
0044 
0045   constexpr std::size_t size() const noexcept {
0046     return m_range.second - m_range.first;
0047   }
0048   constexpr bool empty() const noexcept { return size() == 0; }
0049 
0050   constexpr Derived subrange(index_type offset) const noexcept {
0051     assert(offset <= m_range.second - m_range.first &&
0052            "Subrange offset out of bounds");
0053     return {container(), {m_range.first + offset, m_range.second}};
0054   }
0055   constexpr Derived subrange(index_type offset,
0056                              index_type count) const noexcept {
0057     assert(offset <= m_range.second - m_range.first &&
0058            "Subrange offset out of bounds");
0059     assert(count <= m_range.second - m_range.first - offset &&
0060            "Subrange count out of bounds");
0061     return {container(),
0062             {m_range.first + offset, m_range.first + offset + count}};
0063   }
0064 
0065   constexpr auto front() const noexcept { return container()[m_range.first]; }
0066   constexpr auto back() const noexcept {
0067     return container()[m_range.second - 1];
0068   }
0069 
0070   constexpr auto begin() const noexcept {
0071     return container().begin() + m_range.first;
0072   }
0073   constexpr auto end() const noexcept {
0074     return container().begin() + m_range.second;
0075   }
0076 
0077  private:
0078   container_type *m_container{};
0079   index_range_type m_range{};
0080 };
0081 
0082 }  // namespace Acts