Siva Chandra Reddy | c6bc106 | 2020-01-26 21:50:27 -0800 | [diff] [blame] | 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 | #include "Array.h" |
| 10 | |
| 11 | #include <stddef.h> // For size_t. |
| 12 | |
| 13 | namespace __llvm_libc { |
| 14 | namespace cpp { |
| 15 | |
| 16 | // The implementations of ArrayRef and MutualArrayRef in this file are based |
| 17 | // on the implementations of the types with the same names in |
| 18 | // llvm/ADT/ArrayRef.h. The implementations in this file are of a limited |
| 19 | // functionality, but can be extended in an as needed basis. |
| 20 | |
| 21 | template <typename T> class ArrayRef { |
| 22 | public: |
| 23 | using iterator = const T *; |
| 24 | |
| 25 | private: |
| 26 | const T *Data = nullptr; |
| 27 | size_t Length = 0; |
| 28 | |
| 29 | public: |
| 30 | ArrayRef() = default; |
| 31 | |
| 32 | // From Array. |
| 33 | template <size_t N> |
| 34 | ArrayRef(const Array<T, N> &Arr) : Data(Arr.Data), Length(N) {} |
| 35 | |
| 36 | // Construct an ArrayRef from a single element. |
| 37 | explicit ArrayRef(const T &OneElt) : Data(&OneElt), Length(1) {} |
| 38 | |
| 39 | // Construct an ArrayRef from a pointer and length. |
| 40 | ArrayRef(const T *data, size_t length) : Data(data), Length(length) {} |
| 41 | |
| 42 | // Construct an ArrayRef from a range. |
| 43 | ArrayRef(const T *begin, const T *end) : Data(begin), Length(end - begin) {} |
| 44 | |
| 45 | // Construct an ArrayRef from a C array. |
| 46 | template <size_t N> |
| 47 | constexpr ArrayRef(const T (&Arr)[N]) : Data(Arr), Length(N) {} |
| 48 | |
| 49 | iterator begin() const { return Data; } |
| 50 | iterator end() const { return Data + Length; } |
| 51 | |
| 52 | bool empty() const { return Length == 0; } |
| 53 | |
| 54 | const T *data() const { return Data; } |
| 55 | |
| 56 | size_t size() const { return Length; } |
| 57 | |
| 58 | const T &operator[](size_t Index) const { return Data[Index]; } |
| 59 | }; |
| 60 | |
| 61 | template <typename T> class MutableArrayRef : public ArrayRef<T> { |
| 62 | public: |
| 63 | using iterator = T *; |
| 64 | |
| 65 | // From Array. |
| 66 | template <size_t N> MutableArrayRef(Array<T, N> &Arr) : ArrayRef<T>(Arr) {} |
| 67 | |
| 68 | // Construct from a single element. |
| 69 | explicit MutableArrayRef(T &OneElt) : ArrayRef<T>(OneElt) {} |
| 70 | |
| 71 | // Construct from a pointer and length. |
| 72 | MutableArrayRef(T *data, size_t length) : ArrayRef<T>(data, length) {} |
| 73 | |
| 74 | // Construct from a range. |
| 75 | MutableArrayRef(T *begin, T *end) : ArrayRef<T>(begin, end) {} |
| 76 | |
| 77 | // Construct from a C array. |
| 78 | template <size_t N> |
| 79 | constexpr MutableArrayRef(T (&Arr)[N]) : ArrayRef<T>(Arr) {} |
| 80 | |
| 81 | T *data() const { return const_cast<T *>(ArrayRef<T>::data()); } |
| 82 | |
| 83 | iterator begin() const { return data(); } |
Siva Chandra Reddy | 3302586 | 2020-01-30 09:09:17 -0800 | [diff] [blame] | 84 | iterator end() const { return data() + this->size(); } |
Siva Chandra Reddy | c6bc106 | 2020-01-26 21:50:27 -0800 | [diff] [blame] | 85 | |
| 86 | T &operator[](size_t Index) const { return data()[Index]; } |
| 87 | }; |
| 88 | |
| 89 | } // namespace cpp |
| 90 | } // namespace __llvm_libc |