LCOV - code coverage report
Current view: top level - home/runner/.local/lib/python3.9/site-packages/torch/include/c10/util - ArrayRef.h (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 8 8 100.0 %
Date: 2025-11-25 13:55:50 Functions: 0 0 -

          Line data    Source code
       1             : //===--- ArrayRef.h - Array Reference Wrapper -------------------*- C++ -*-===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : 
      10             : // ATen: modified from llvm::ArrayRef.
      11             : // removed llvm-specific functionality
      12             : // removed some implicit const -> non-const conversions that rely on
      13             : // complicated std::enable_if meta-programming
      14             : // removed a bunch of slice variants for simplicity...
      15             : 
      16             : #pragma once
      17             : 
      18             : #include <c10/macros/Macros.h>
      19             : #include <c10/util/Deprecated.h>
      20             : #include <c10/util/Exception.h>
      21             : #include <c10/util/SmallVector.h>
      22             : 
      23             : #include <array>
      24             : #include <cstddef>
      25             : #include <cstdint>
      26             : #include <initializer_list>
      27             : #include <iterator>
      28             : #include <ostream>
      29             : #include <type_traits>
      30             : #include <vector>
      31             : 
      32             : namespace c10 {
      33             : /// ArrayRef - Represent a constant reference to an array (0 or more elements
      34             : /// consecutively in memory), i.e. a start pointer and a length.  It allows
      35             : /// various APIs to take consecutive elements easily and conveniently.
      36             : ///
      37             : /// This class does not own the underlying data, it is expected to be used in
      38             : /// situations where the data resides in some other buffer, whose lifetime
      39             : /// extends past that of the ArrayRef. For this reason, it is not in general
      40             : /// safe to store an ArrayRef.
      41             : ///
      42             : /// This is intended to be trivially copyable, so it should be passed by
      43             : /// value.
      44             : template <typename T>
      45             : class ArrayRef final {
      46             :  public:
      47             :   using iterator = const T*;
      48             :   using const_iterator = const T*;
      49             :   using size_type = size_t;
      50             :   using value_type = T;
      51             : 
      52             :   using reverse_iterator = std::reverse_iterator<iterator>;
      53             : 
      54             :  private:
      55             :   /// The start of the array, in an external buffer.
      56             :   const T* Data;
      57             : 
      58             :   /// The number of elements.
      59             :   size_type Length;
      60             : 
      61             :   void debugCheckNullptrInvariant() {
      62             :     TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
      63             :         Data != nullptr || Length == 0,
      64             :         "created ArrayRef with nullptr and non-zero length! std::optional relies on this being illegal");
      65             :   }
      66             : 
      67             :  public:
      68             :   /// @name Constructors
      69             :   /// @{
      70             : 
      71             :   /// Construct an empty ArrayRef.
      72           7 :   /* implicit */ constexpr ArrayRef() : Data(nullptr), Length(0) {}
      73             : 
      74             :   /// Construct an ArrayRef from a single element.
      75             :   // TODO Make this explicit
      76             :   constexpr ArrayRef(const T& OneElt) : Data(&OneElt), Length(1) {}
      77             : 
      78             :   /// Construct an ArrayRef from a pointer and length.
      79             :   constexpr ArrayRef(const T* data, size_t length)
      80             :       : Data(data), Length(length) {
      81             :     debugCheckNullptrInvariant();
      82             :   }
      83             : 
      84             :   /// Construct an ArrayRef from a range.
      85             :   constexpr ArrayRef(const T* begin, const T* end)
      86             :       : Data(begin), Length(end - begin) {
      87             :     debugCheckNullptrInvariant();
      88             :   }
      89             : 
      90             :   /// Construct an ArrayRef from a SmallVector. This is templated in order to
      91             :   /// avoid instantiating SmallVectorTemplateCommon<T> whenever we
      92             :   /// copy-construct an ArrayRef.
      93             :   template <typename U>
      94             :   /* implicit */ ArrayRef(const SmallVectorTemplateCommon<T, U>& Vec)
      95             :       : Data(Vec.data()), Length(Vec.size()) {
      96             :     debugCheckNullptrInvariant();
      97             :   }
      98             : 
      99             :   template <
     100             :       typename Container,
     101             :       typename U = decltype(std::declval<Container>().data()),
     102             :       typename = std::enable_if_t<
     103             :           (std::is_same_v<U, T*> || std::is_same_v<U, T const*>)>>
     104             :   /* implicit */ ArrayRef(const Container& container)
     105             :       : Data(container.data()), Length(container.size()) {
     106             :     debugCheckNullptrInvariant();
     107             :   }
     108             : 
     109             :   /// Construct an ArrayRef from a std::vector.
     110             :   // The enable_if stuff here makes sure that this isn't used for
     111             :   // std::vector<bool>, because ArrayRef can't work on a std::vector<bool>
     112             :   // bitfield.
     113             :   template <typename A>
     114          78 :   /* implicit */ ArrayRef(const std::vector<T, A>& Vec)
     115          78 :       : Data(Vec.data()), Length(Vec.size()) {
     116             :     static_assert(
     117             :         !std::is_same_v<T, bool>,
     118             :         "ArrayRef<bool> cannot be constructed from a std::vector<bool> bitfield.");
     119             :   }
     120             : 
     121             :   /// Construct an ArrayRef from a std::array
     122             :   template <size_t N>
     123             :   /* implicit */ constexpr ArrayRef(const std::array<T, N>& Arr)
     124             :       : Data(Arr.data()), Length(N) {}
     125             : 
     126             :   /// Construct an ArrayRef from a C array.
     127             :   template <size_t N>
     128             :   // NOLINTNEXTLINE(*c-arrays*)
     129             :   /* implicit */ constexpr ArrayRef(const T (&Arr)[N]) : Data(Arr), Length(N) {}
     130             : 
     131             :   /// Construct an ArrayRef from a std::initializer_list.
     132         264 :   /* implicit */ constexpr ArrayRef(const std::initializer_list<T>& Vec)
     133         264 :       : Data(
     134             :             std::begin(Vec) == std::end(Vec) ? static_cast<T*>(nullptr)
     135             :                                              : std::begin(Vec)),
     136         188 :         Length(Vec.size()) {}
     137             : 
     138             :   /// @}
     139             :   /// @name Simple Operations
     140             :   /// @{
     141             : 
     142             :   constexpr iterator begin() const {
     143             :     return Data;
     144             :   }
     145             :   constexpr iterator end() const {
     146         264 :     return Data + Length;
     147             :   }
     148             : 
     149             :   // These are actually the same as iterator, since ArrayRef only
     150             :   // gives you const iterators.
     151             :   constexpr const_iterator cbegin() const {
     152             :     return Data;
     153             :   }
     154             :   constexpr const_iterator cend() const {
     155             :     return Data + Length;
     156             :   }
     157             : 
     158             :   constexpr reverse_iterator rbegin() const {
     159             :     return reverse_iterator(end());
     160             :   }
     161             :   constexpr reverse_iterator rend() const {
     162             :     return reverse_iterator(begin());
     163             :   }
     164             : 
     165             :   /// Check if all elements in the array satisfy the given expression
     166             :   constexpr bool allMatch(const std::function<bool(const T&)>& pred) const {
     167             :     return std::all_of(cbegin(), cend(), pred);
     168             :   }
     169             : 
     170             :   /// empty - Check if the array is empty.
     171             :   constexpr bool empty() const {
     172             :     return Length == 0;
     173             :   }
     174             : 
     175             :   constexpr const T* data() const {
     176             :     return Data;
     177             :   }
     178             : 
     179             :   /// size - Get the array size.
     180             :   constexpr size_t size() const {
     181          55 :     return Length;
     182             :   }
     183             : 
     184             :   /// front - Get the first element.
     185             :   constexpr const T& front() const {
     186             :     TORCH_CHECK(
     187             :         !empty(), "ArrayRef: attempted to access front() of empty list");
     188             :     return Data[0];
     189             :   }
     190             : 
     191             :   /// back - Get the last element.
     192             :   constexpr const T& back() const {
     193             :     TORCH_CHECK(!empty(), "ArrayRef: attempted to access back() of empty list");
     194             :     return Data[Length - 1];
     195             :   }
     196             : 
     197             :   /// equals - Check for element-wise equality.
     198             :   constexpr bool equals(ArrayRef RHS) const {
     199             :     return Length == RHS.Length && std::equal(begin(), end(), RHS.begin());
     200             :   }
     201             : 
     202             :   /// slice(n, m) - Take M elements of the array starting at element N
     203             :   constexpr ArrayRef<T> slice(size_t N, size_t M) const {
     204             :     TORCH_CHECK(
     205             :         N + M <= size(),
     206             :         "ArrayRef: invalid slice, N = ",
     207             :         N,
     208             :         "; M = ",
     209             :         M,
     210             :         "; size = ",
     211             :         size());
     212             :     return ArrayRef<T>(data() + N, M);
     213             :   }
     214             : 
     215             :   /// slice(n) - Chop off the first N elements of the array.
     216             :   constexpr ArrayRef<T> slice(size_t N) const {
     217             :     TORCH_CHECK(
     218             :         N <= size(), "ArrayRef: invalid slice, N = ", N, "; size = ", size());
     219             :     return slice(N, size() - N);
     220             :   }
     221             : 
     222             :   /// @}
     223             :   /// @name Operator Overloads
     224             :   /// @{
     225             :   constexpr const T& operator[](size_t Index) const {
     226             :     return Data[Index];
     227             :   }
     228             : 
     229             :   /// Vector compatibility
     230             :   constexpr const T& at(size_t Index) const {
     231             :     TORCH_CHECK(
     232             :         Index < Length,
     233             :         "ArrayRef: invalid index Index = ",
     234             :         Index,
     235             :         "; Length = ",
     236             :         Length);
     237             :     return Data[Index];
     238             :   }
     239             : 
     240             :   /// Disallow accidental assignment from a temporary.
     241             :   ///
     242             :   /// The declaration here is extra complicated so that "arrayRef = {}"
     243             :   /// continues to select the move assignment operator.
     244             :   template <typename U>
     245             :   std::enable_if_t<std::is_same_v<U, T>, ArrayRef<T>>& operator=(
     246             :       // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
     247             :       U&& Temporary) = delete;
     248             : 
     249             :   /// Disallow accidental assignment from a temporary.
     250             :   ///
     251             :   /// The declaration here is extra complicated so that "arrayRef = {}"
     252             :   /// continues to select the move assignment operator.
     253             :   template <typename U>
     254             :   std::enable_if_t<std::is_same_v<U, T>, ArrayRef<T>>& operator=(
     255             :       std::initializer_list<U>) = delete;
     256             : 
     257             :   /// @}
     258             :   /// @name Expensive Operations
     259             :   /// @{
     260             :   std::vector<T> vec() const {
     261             :     return std::vector<T>(Data, Data + Length);
     262             :   }
     263             : 
     264             :   /// @}
     265             : };
     266             : 
     267             : template <typename T>
     268             : std::ostream& operator<<(std::ostream& out, ArrayRef<T> list) {
     269             :   int i = 0;
     270             :   out << "[";
     271             :   for (const auto& e : list) {
     272             :     if (i++ > 0)
     273             :       out << ", ";
     274             :     out << e;
     275             :   }
     276             :   out << "]";
     277             :   return out;
     278             : }
     279             : 
     280             : /// @name ArrayRef Convenience constructors
     281             : /// @{
     282             : 
     283             : /// Construct an ArrayRef from a single element.
     284             : template <typename T>
     285             : ArrayRef<T> makeArrayRef(const T& OneElt) {
     286             :   return OneElt;
     287             : }
     288             : 
     289             : /// Construct an ArrayRef from a pointer and length.
     290             : template <typename T>
     291             : ArrayRef<T> makeArrayRef(const T* data, size_t length) {
     292             :   return ArrayRef<T>(data, length);
     293             : }
     294             : 
     295             : /// Construct an ArrayRef from a range.
     296             : template <typename T>
     297             : ArrayRef<T> makeArrayRef(const T* begin, const T* end) {
     298             :   return ArrayRef<T>(begin, end);
     299             : }
     300             : 
     301             : /// Construct an ArrayRef from a SmallVector.
     302             : template <typename T>
     303             : ArrayRef<T> makeArrayRef(const SmallVectorImpl<T>& Vec) {
     304             :   return Vec;
     305             : }
     306             : 
     307             : /// Construct an ArrayRef from a SmallVector.
     308             : template <typename T, unsigned N>
     309             : ArrayRef<T> makeArrayRef(const SmallVector<T, N>& Vec) {
     310             :   return Vec;
     311             : }
     312             : 
     313             : /// Construct an ArrayRef from a std::vector.
     314             : template <typename T>
     315             : ArrayRef<T> makeArrayRef(const std::vector<T>& Vec) {
     316             :   return Vec;
     317             : }
     318             : 
     319             : /// Construct an ArrayRef from a std::array.
     320             : template <typename T, std::size_t N>
     321             : ArrayRef<T> makeArrayRef(const std::array<T, N>& Arr) {
     322             :   return Arr;
     323             : }
     324             : 
     325             : /// Construct an ArrayRef from an ArrayRef (no-op) (const)
     326             : template <typename T>
     327             : ArrayRef<T> makeArrayRef(const ArrayRef<T>& Vec) {
     328             :   return Vec;
     329             : }
     330             : 
     331             : /// Construct an ArrayRef from an ArrayRef (no-op)
     332             : template <typename T>
     333             : ArrayRef<T>& makeArrayRef(ArrayRef<T>& Vec) {
     334             :   return Vec;
     335             : }
     336             : 
     337             : /// Construct an ArrayRef from a C array.
     338             : template <typename T, size_t N>
     339             : // NOLINTNEXTLINE(*c-arrays*)
     340             : ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {
     341             :   return ArrayRef<T>(Arr);
     342             : }
     343             : 
     344             : // WARNING: Template instantiation will NOT be willing to do an implicit
     345             : // conversions to get you to an c10::ArrayRef, which is why we need so
     346             : // many overloads.
     347             : 
     348             : template <typename T>
     349             : bool operator==(c10::ArrayRef<T> a1, c10::ArrayRef<T> a2) {
     350             :   return a1.equals(a2);
     351             : }
     352             : 
     353             : template <typename T>
     354             : bool operator!=(c10::ArrayRef<T> a1, c10::ArrayRef<T> a2) {
     355             :   return !a1.equals(a2);
     356             : }
     357             : 
     358             : template <typename T>
     359             : bool operator==(const std::vector<T>& a1, c10::ArrayRef<T> a2) {
     360             :   return c10::ArrayRef<T>(a1).equals(a2);
     361             : }
     362             : 
     363             : template <typename T>
     364             : bool operator!=(const std::vector<T>& a1, c10::ArrayRef<T> a2) {
     365             :   return !c10::ArrayRef<T>(a1).equals(a2);
     366             : }
     367             : 
     368             : template <typename T>
     369             : bool operator==(c10::ArrayRef<T> a1, const std::vector<T>& a2) {
     370             :   return a1.equals(c10::ArrayRef<T>(a2));
     371             : }
     372             : 
     373             : template <typename T>
     374             : bool operator!=(c10::ArrayRef<T> a1, const std::vector<T>& a2) {
     375             :   return !a1.equals(c10::ArrayRef<T>(a2));
     376             : }
     377             : 
     378             : using IntArrayRef = ArrayRef<int64_t>;
     379             : 
     380             : // This alias is deprecated because it doesn't make ownership
     381             : // semantics obvious.  Use IntArrayRef instead!
     382             : C10_DEFINE_DEPRECATED_USING(IntList, ArrayRef<int64_t>)
     383             : 
     384             : } // namespace c10

Generated by: LCOV version 1.16