Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:42:43

0001 // Copyright (c) ONNX Project Contributors
0002 
0003 /*
0004  * SPDX-License-Identifier: Apache-2.0
0005  */
0006 
0007 // ATTENTION: The code in this file is highly EXPERIMENTAL.
0008 // Adventurous users should note that the APIs will probably change.
0009 
0010 //===--- ArrayRef.h - Array Reference Wrapper -------------------*- C++ -*-===//
0011 //
0012 //                     The LLVM Compiler Infrastructure
0013 //
0014 // This file is distributed under the University of Illinois Open Source
0015 // License. See LICENSE.TXT for details.
0016 //
0017 //===----------------------------------------------------------------------===//
0018 
0019 // ONNX: modified from llvm::ArrayRef.
0020 // removed llvm-specific functionality
0021 // removed some implicit const -> non-const conversions that rely on
0022 // complicated std::enable_if meta-programming
0023 // removed a bunch of slice variants for simplicity...
0024 
0025 #pragma once
0026 #include <assert.h>
0027 
0028 #include <array>
0029 #include <vector>
0030 
0031 namespace ONNX_NAMESPACE {
0032 /// ArrayRef - Represent a constant reference to an array (0 or more elements
0033 /// consecutively in memory), i.e. a start pointer and a length.  It allows
0034 /// various APIs to take consecutive elements easily and conveniently.
0035 ///
0036 /// This class does not own the underlying data, it is expected to be used in
0037 /// situations where the data resides in some other buffer, whose lifetime
0038 /// extends past that of the ArrayRef. For this reason, it is not in general
0039 /// safe to store an ArrayRef.
0040 ///
0041 /// This is intended to be trivially copyable, so it should be passed by
0042 /// value.
0043 template <typename T>
0044 class ArrayRef {
0045  public:
0046   typedef const T* iterator;
0047   typedef const T* const_iterator;
0048   typedef size_t size_type;
0049 
0050   typedef std::reverse_iterator<iterator> reverse_iterator;
0051 
0052  private:
0053   /// The start of the array, in an external buffer.
0054   const T* Data;
0055 
0056   /// The number of elements.
0057   size_type Length;
0058 
0059  public:
0060   /// @name Constructors
0061   /// @{
0062 
0063   /// Construct an empty ArrayRef.
0064   /*implicit*/ ArrayRef() : Data(nullptr), Length(0) {}
0065 
0066   /// Construct an ArrayRef from a single element.
0067   /*implicit*/ ArrayRef(const T& OneElt) : Data(&OneElt), Length(1) {}
0068 
0069   /// Construct an ArrayRef from a pointer and length.
0070   /*implicit*/ ArrayRef(const T* data, size_t length) : Data(data), Length(length) {}
0071 
0072   /// Construct an ArrayRef from a range.
0073   ArrayRef(const T* begin, const T* end) : Data(begin), Length(end - begin) {}
0074 
0075   /// Construct an ArrayRef from a std::vector.
0076   template <typename A>
0077   /*implicit*/ ArrayRef(const std::vector<T, A>& Vec) : Data(Vec.data()), Length(Vec.size()) {}
0078 
0079   /// Construct an ArrayRef from a std::array
0080   template <size_t N>
0081   /*implicit*/ constexpr ArrayRef(const std::array<T, N>& Arr) : Data(Arr.data()), Length(N) {}
0082 
0083   /// Construct an ArrayRef from a C array.
0084   template <size_t N>
0085   /*implicit*/ constexpr ArrayRef(const T (&Arr)[N]) : Data(Arr), Length(N) {}
0086 
0087   /// Construct an ArrayRef from a std::initializer_list.
0088   /*implicit*/ ArrayRef(const std::initializer_list<T>& Vec)
0089       : Data(Vec.begin() == Vec.end() ? (T*)nullptr : Vec.begin()), Length(Vec.size()) {}
0090 
0091   /// @}
0092   /// @name Simple Operations
0093   /// @{
0094 
0095   iterator begin() const {
0096     return Data;
0097   }
0098   iterator end() const {
0099     return Data + Length;
0100   }
0101 
0102   reverse_iterator rbegin() const {
0103     return reverse_iterator(end());
0104   }
0105   reverse_iterator rend() const {
0106     return reverse_iterator(begin());
0107   }
0108 
0109   /// empty - Check if the array is empty.
0110   bool empty() const {
0111     return Length == 0;
0112   }
0113 
0114   const T* data() const {
0115     return Data;
0116   }
0117 
0118   /// size - Get the array size.
0119   size_t size() const {
0120     return Length;
0121   }
0122 
0123   /// front - Get the first element.
0124   const T& front() const {
0125     assert(!empty());
0126     return Data[0];
0127   }
0128 
0129   /// back - Get the last element.
0130   const T& back() const {
0131     assert(!empty());
0132     return Data[Length - 1];
0133   }
0134 
0135   /// equals - Check for element-wise equality.
0136   bool equals(ArrayRef RHS) const {
0137     if (Length != RHS.Length)
0138       return false;
0139     return std::equal(begin(), end(), RHS.begin());
0140   }
0141 
0142   /// slice(n, m) - Chop off the first N elements of the array, and keep M
0143   /// elements in the array.
0144   ArrayRef<T> slice(size_t N, size_t M) const {
0145     assert(N + M <= size() && "Invalid specifier");
0146     return ArrayRef<T>(data() + N, M);
0147   }
0148 
0149   /// slice(n) - Chop off the first N elements of the array.
0150   ArrayRef<T> slice(size_t N) const {
0151     return slice(N, size() - N);
0152   }
0153 
0154   /// @}
0155   /// @name Operator Overloads
0156   /// @{
0157   const T& operator[](size_t Index) const {
0158     assert(Index < Length && "Invalid index!");
0159     return Data[Index];
0160   }
0161 
0162   /// Vector compatibility
0163   const T& at(size_t Index) const {
0164     assert(Index < Length && "Invalid index!");
0165     return Data[Index];
0166   }
0167 
0168   /// Disallow accidental assignment from a temporary.
0169   ///
0170   /// The declaration here is extra complicated so that "arrayRef = {}"
0171   /// continues to select the move assignment operator.
0172   template <typename U>
0173   typename std::enable_if<std::is_same<U, T>::value, ArrayRef<T>>::type& operator=(U&& Temporary) = delete;
0174 
0175   /// Disallow accidental assignment from a temporary.
0176   ///
0177   /// The declaration here is extra complicated so that "arrayRef = {}"
0178   /// continues to select the move assignment operator.
0179   template <typename U>
0180   typename std::enable_if<std::is_same<U, T>::value, ArrayRef<T>>::type& operator=(std::initializer_list<U>) = delete;
0181 
0182   /// @}
0183   /// @name Expensive Operations
0184   /// @{
0185   std::vector<T> vec() const {
0186     return std::vector<T>(Data, Data + Length);
0187   }
0188 
0189   /// @}
0190   /// @name Conversion operators
0191   /// @{
0192   operator std::vector<T>() const {
0193     return std::vector<T>(Data, Data + Length);
0194   }
0195 
0196   /// @}
0197 };
0198 
0199 } // namespace ONNX_NAMESPACE