blob: ea58245daa1b1d42107d2d1b13cd4fd2a0a960aa [file] [log] [blame]
Siva Chandra Reddyc6bc1062020-01-26 21:50:27 -08001//===----------------- 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
13namespace __llvm_libc {
14namespace 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
21template <typename T> class ArrayRef {
22public:
23 using iterator = const T *;
24
25private:
26 const T *Data = nullptr;
27 size_t Length = 0;
28
29public:
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
61template <typename T> class MutableArrayRef : public ArrayRef<T> {
62public:
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 Reddy33025862020-01-30 09:09:17 -080084 iterator end() const { return data() + this->size(); }
Siva Chandra Reddyc6bc1062020-01-26 21:50:27 -080085
86 T &operator[](size_t Index) const { return data()[Index]; }
87};
88
89} // namespace cpp
90} // namespace __llvm_libc