:github_url: https://github.com/pytorch/pytorch .. _program_listing_file_c10_util_ArrayRef.h: Program Listing for File ArrayRef.h =================================== |exhale_lsh| :ref:`Return to documentation for file ` (``c10/util/ArrayRef.h``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp //===--- ArrayRef.h - Array Reference Wrapper -------------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // ATen: modified from llvm::ArrayRef. // removed llvm-specific functionality // removed some implicit const -> non-const conversions that rely on // complicated std::enable_if meta-programming // removed a bunch of slice variants for simplicity... #pragma once #include #include #include #include #include #include #include #include #include #include #include #include namespace c10 { template // ArrayRef cannot be derived from. Normally, we would use `final` // specifier to force this constraint at compile time. However, Intel // compiler does not recognize ArrayRef as a class template (which is // required in the definition of at::TensorAccessor, for instance) // when `final` specifier is used. So, we cannot define ArrayRef as // final because of the Intel compiler issue. class ArrayRef : public HeaderOnlyArrayRef { public: using HeaderOnlyArrayRef::HeaderOnlyArrayRef; template /* implicit */ ArrayRef(const SmallVectorTemplateCommon& Vec) : HeaderOnlyArrayRef(Vec.data(), Vec.size()) {} constexpr const T& front() const { TORCH_CHECK( !this->empty(), "ArrayRef: attempted to access front() of empty list"); return this->Data[0]; } constexpr const T& back() const { TORCH_CHECK( !this->empty(), "ArrayRef: attempted to access back() of empty list"); return this->Data[this->Length - 1]; } constexpr ArrayRef slice(size_t N, size_t M) const { TORCH_CHECK( N + M <= this->size(), "ArrayRef: invalid slice, N = ", N, "; M = ", M, "; size = ", this->size()); return ArrayRef(this->data() + N, M); } constexpr ArrayRef slice(size_t N) const { TORCH_CHECK( N <= this->size(), "ArrayRef: invalid slice, N = ", N, "; size = ", this->size()); return slice(N, this->size() - N); // should this slice be this->slice? } constexpr const T& at(size_t Index) const { TORCH_CHECK( Index < this->Length, "ArrayRef: invalid index Index = ", Index, "; Length = ", this->Length); return this->Data[Index]; } template std::enable_if_t, ArrayRef>& operator=( // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward) U&& Temporary) = delete; template std::enable_if_t, ArrayRef>& operator=( std::initializer_list) = delete; }; // Single element constructor template ArrayRef(const T&) -> ArrayRef; // Pointer and length constructor template ArrayRef(const T*, size_t) -> ArrayRef; // Range constructor (begin, end) template ArrayRef(const T*, const T*) -> ArrayRef; // Generic container constructor (anything with .data() and .size()) template ArrayRef(const Container&) -> ArrayRef< std::remove_pointer_t().data())>>; // std::vector constructor template ArrayRef(const std::vector&) -> ArrayRef; // std::array constructor template ArrayRef(const std::array&) -> ArrayRef; // C array constructor template ArrayRef(const T (&)[N]) -> ArrayRef; // std::initializer_list constructor template ArrayRef(const std::initializer_list&) -> ArrayRef; template std::ostream& operator<<(std::ostream& out, ArrayRef list) { int i = 0; out << '['; for (const auto& e : list) { if (i++ > 0) out << ", "; out << e; } out << ']'; return out; } template ArrayRef makeArrayRef(const T& OneElt) { return OneElt; } template ArrayRef makeArrayRef(const T* data, size_t length) { return ArrayRef(data, length); } template ArrayRef makeArrayRef(const T* begin, const T* end) { return ArrayRef(begin, end); } template ArrayRef makeArrayRef(const SmallVectorImpl& Vec) { return Vec; } template ArrayRef makeArrayRef(const SmallVector& Vec) { return Vec; } template ArrayRef makeArrayRef(const std::vector& Vec) { return Vec; } template ArrayRef makeArrayRef(const std::array& Arr) { return Arr; } template ArrayRef makeArrayRef(const ArrayRef& Vec) { return Vec; } template ArrayRef& makeArrayRef(ArrayRef& Vec) { return Vec; } template // NOLINTNEXTLINE(*c-arrays*) ArrayRef makeArrayRef(const T (&Arr)[N]) { return ArrayRef(Arr); } // WARNING: Template instantiation will NOT be willing to do an implicit // conversions to get you to an c10::ArrayRef, which is why we need so // many overloads. template bool operator==(c10::ArrayRef a1, c10::ArrayRef a2) { return a1.equals(a2); } template bool operator!=(c10::ArrayRef a1, c10::ArrayRef a2) { return !a1.equals(a2); } template bool operator==(const std::vector& a1, c10::ArrayRef a2) { return c10::ArrayRef(a1).equals(a2); } template bool operator!=(const std::vector& a1, c10::ArrayRef a2) { return !c10::ArrayRef(a1).equals(a2); } template bool operator==(c10::ArrayRef a1, const std::vector& a2) { return a1.equals(c10::ArrayRef(a2)); } template bool operator!=(c10::ArrayRef a1, const std::vector& a2) { return !a1.equals(c10::ArrayRef(a2)); } using IntArrayRef = ArrayRef; using IntList [[deprecated( "This alias is deprecated because it doesn't make ownership semantics obvious. Use IntArrayRef instead!")]] = ArrayRef; } // namespace c10