1 //===-- Self contained ArrayRef type ----------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLVM_LIBC_UTILS_CPP_ARRAYREF_H 10 #define LLVM_LIBC_UTILS_CPP_ARRAYREF_H 11 12 #include "Array.h" 13 14 #include <stddef.h> // For size_t. 15 16 namespace __llvm_libc { 17 namespace cpp { 18 19 // The implementations of ArrayRef and MutualArrayRef in this file are based 20 // on the implementations of the types with the same names in 21 // llvm/ADT/ArrayRef.h. The implementations in this file are of a limited 22 // functionality, but can be extended in an as needed basis. 23 24 template <typename T> class ArrayRef { 25 public: 26 using iterator = const T *; 27 28 private: 29 const T *Data = nullptr; 30 size_t Length = 0; 31 32 public: 33 ArrayRef() = default; 34 35 // From Array. 36 template <size_t N> ArrayRef(const Array<T,N> & Arr)37 ArrayRef(const Array<T, N> &Arr) : Data(Arr.Data), Length(N) {} 38 39 // Construct an ArrayRef from a single element. ArrayRef(const T & OneElt)40 explicit ArrayRef(const T &OneElt) : Data(&OneElt), Length(1) {} 41 42 // Construct an ArrayRef from a pointer and length. ArrayRef(const T * data,size_t length)43 ArrayRef(const T *data, size_t length) : Data(data), Length(length) {} 44 45 // Construct an ArrayRef from a range. ArrayRef(const T * begin,const T * end)46 ArrayRef(const T *begin, const T *end) : Data(begin), Length(end - begin) {} 47 48 // Construct an ArrayRef from a C array. 49 template <size_t N> ArrayRef(const T (& Arr)[N])50 constexpr ArrayRef(const T (&Arr)[N]) : Data(Arr), Length(N) {} 51 begin()52 iterator begin() const { return Data; } end()53 iterator end() const { return Data + Length; } 54 empty()55 bool empty() const { return Length == 0; } 56 data()57 const T *data() const { return Data; } 58 size()59 size_t size() const { return Length; } 60 61 const T &operator[](size_t Index) const { return Data[Index]; } 62 }; 63 64 template <typename T> class MutableArrayRef : public ArrayRef<T> { 65 public: 66 using iterator = T *; 67 68 // From Array. MutableArrayRef(Array<T,N> & Arr)69 template <size_t N> MutableArrayRef(Array<T, N> &Arr) : ArrayRef<T>(Arr) {} 70 71 // Construct from a single element. MutableArrayRef(T & OneElt)72 explicit MutableArrayRef(T &OneElt) : ArrayRef<T>(OneElt) {} 73 74 // Construct from a pointer and length. MutableArrayRef(T * data,size_t length)75 MutableArrayRef(T *data, size_t length) : ArrayRef<T>(data, length) {} 76 77 // Construct from a range. MutableArrayRef(T * begin,T * end)78 MutableArrayRef(T *begin, T *end) : ArrayRef<T>(begin, end) {} 79 80 // Construct from a C array. 81 template <size_t N> MutableArrayRef(T (& Arr)[N])82 constexpr MutableArrayRef(T (&Arr)[N]) : ArrayRef<T>(Arr) {} 83 data()84 T *data() const { return const_cast<T *>(ArrayRef<T>::data()); } 85 begin()86 iterator begin() const { return data(); } end()87 iterator end() const { return data() + this->size(); } 88 89 T &operator[](size_t Index) const { return data()[Index]; } 90 }; 91 92 } // namespace cpp 93 } // namespace __llvm_libc 94 95 #endif // LLVM_LIBC_UTILS_CPP_ARRAYREF_H 96