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