//===--- 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 { /// ArrayRef - Represent a constant reference to an array (0 or more elements /// consecutively in memory), i.e. a start pointer and a length. It allows /// various APIs to take consecutive elements easily and conveniently. /// /// This class does not own the underlying data, it is expected to be used in /// situations where the data resides in some other buffer, whose lifetime /// extends past that of the ArrayRef. For this reason, it is not in general /// safe to store an ArrayRef. /// /// This is intended to be trivially copyable, so it should be passed by /// value. /// /// NOTE: We have refactored out the headeronly parts of the ArrayRef struct /// into HeaderOnlyArrayRef. As adding `virtual` would change the performance of /// the underlying constexpr calls, we rely on apparent-type dispatch for /// inheritance. This should be fine because their memory format is the same, /// and it is never incorrect for ArrayRef to call HeaderOnlyArrayRef methods. /// However, you should prefer to use ArrayRef when possible, because its use /// of TORCH_CHECK will lead to better user-facing error messages. template class ArrayRef final : public HeaderOnlyArrayRef { public: /// @name Constructors, all inherited from HeaderOnlyArrayRef except for /// SmallVector. /// @{ using HeaderOnlyArrayRef::HeaderOnlyArrayRef; /// Construct an ArrayRef from a std::vector. /// This constructor is identical to the one in HeaderOnlyArrayRef, but we /// include it to help with Class Template Argument Deduction (CTAD). /// Without it, CTAD can fail sometimes due to the indirect constructor /// inheritance. So we explicitly include this constructor. template /* implicit */ ArrayRef(const std::vector& Vec) : HeaderOnlyArrayRef(Vec.data(), Vec.size()) {} /// Construct an ArrayRef from a SmallVector. This is templated in order to /// avoid instantiating SmallVectorTemplateCommon whenever we /// copy-construct an ArrayRef. /// NOTE: this is the only constructor that is not inherited from /// HeaderOnlyArrayRef. template /* implicit */ ArrayRef(const SmallVectorTemplateCommon& Vec) : HeaderOnlyArrayRef(Vec.data(), Vec.size()) {} /// @} /// @name Simple Operations, mostly inherited from HeaderOnlyArrayRef /// @{ /// front - Get the first element. /// We deviate from HeaderOnlyArrayRef by using TORCH_CHECK instead of /// STD_TORCH_CHECK constexpr const T& front() const { TORCH_CHECK( !this->empty(), "ArrayRef: attempted to access front() of empty list"); return this->Data[0]; } /// back - Get the last element. /// We deviate from HeaderOnlyArrayRef by using TORCH_CHECK instead of /// STD_TORCH_CHECK constexpr const T& back() const { TORCH_CHECK( !this->empty(), "ArrayRef: attempted to access back() of empty list"); return this->Data[this->Length - 1]; } /// slice(n, m) - Take M elements of the array starting at element N /// We deviate from HeaderOnlyArrayRef by using TORCH_CHECK instead of /// STD_TORCH_CHECK 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); } /// slice(n) - Chop off the first N elements of the array. /// We deviate from HeaderOnlyArrayRef by using TORCH_CHECK instead of /// STD_TORCH_CHECK 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? } /// @} /// @name Operator Overloads /// @{ /// Vector compatibility /// We deviate from HeaderOnlyArrayRef by using TORCH_CHECK instead of /// STD_TORCH_CHECK 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]; } /// Disallow accidental assignment from a temporary. /// /// The declaration here is extra complicated so that "arrayRef = {}" /// continues to select the move assignment operator. template std::enable_if_t, ArrayRef>& operator=( // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward) U&& Temporary) = delete; /// Disallow accidental assignment from a temporary. /// /// The declaration here is extra complicated so that "arrayRef = {}" /// continues to select the move assignment operator. template std::enable_if_t, ArrayRef>& operator=( std::initializer_list) = delete; /// @} }; 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; } /// @name ArrayRef Convenience constructors /// @{ /// Construct an ArrayRef from a single element. template ArrayRef makeArrayRef(const T& OneElt) { return OneElt; } /// Construct an ArrayRef from a pointer and length. template ArrayRef makeArrayRef(const T* data, size_t length) { return ArrayRef(data, length); } /// Construct an ArrayRef from a range. template ArrayRef makeArrayRef(const T* begin, const T* end) { return ArrayRef(begin, end); } /// Construct an ArrayRef from a SmallVector. template ArrayRef makeArrayRef(const SmallVectorImpl& Vec) { return Vec; } /// Construct an ArrayRef from a SmallVector. template ArrayRef makeArrayRef(const SmallVector& Vec) { return Vec; } /// Construct an ArrayRef from a std::vector. template ArrayRef makeArrayRef(const std::vector& Vec) { return Vec; } /// Construct an ArrayRef from a std::array. template ArrayRef makeArrayRef(const std::array& Arr) { return Arr; } /// Construct an ArrayRef from an ArrayRef (no-op) (const) template ArrayRef makeArrayRef(const ArrayRef& Vec) { return Vec; } /// Construct an ArrayRef from an ArrayRef (no-op) template ArrayRef& makeArrayRef(ArrayRef& Vec) { return Vec; } /// Construct an ArrayRef from a C array. 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