Skip to content

Commit 9b70b84

Browse files
[ADT] Implement EnumeratedArray with std::array (NFC) (#158407)
EnumeratedArray provides an std::array-like interface except that you access the array with an enum index. Now, the problem is that because the underlying array is implemented as a C array, we have to mirror what std::array would do: iterator end() { return begin() + size(); } reverse_iterator rbegin() { return reverse_iterator(end()); } This patch switches to the std::array. This way, we just have to "forward" calls to begin, end, rbegin, rend, etc. Also, we benefit from std::array::fill in one of the constructors.
1 parent 30e9cba commit 9b70b84

File tree

1 file changed

+20
-30
lines changed

1 file changed

+20
-30
lines changed

llvm/include/llvm/ADT/EnumeratedArray.h

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,25 @@
1515
#ifndef LLVM_ADT_ENUMERATEDARRAY_H
1616
#define LLVM_ADT_ENUMERATEDARRAY_H
1717

18+
#include "llvm/ADT/STLExtras.h"
19+
#include <array>
1820
#include <cassert>
19-
#include <iterator>
2021

2122
namespace llvm {
2223

2324
template <typename ValueType, typename Enumeration,
2425
Enumeration LargestEnum = Enumeration::Last, typename IndexType = int,
2526
IndexType Size = 1 + static_cast<IndexType>(LargestEnum)>
2627
class EnumeratedArray {
27-
public:
28-
using iterator = ValueType *;
29-
using const_iterator = const ValueType *;
28+
static_assert(Size > 0);
29+
using ArrayTy = std::array<ValueType, Size>;
30+
ArrayTy Underlying;
3031

31-
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
32-
using reverse_iterator = std::reverse_iterator<iterator>;
32+
public:
33+
using iterator = typename ArrayTy::iterator;
34+
using const_iterator = typename ArrayTy::const_iterator;
35+
using reverse_iterator = typename ArrayTy::reverse_iterator;
36+
using const_reverse_iterator = typename ArrayTy::const_reverse_iterator;
3337

3438
using value_type = ValueType;
3539
using reference = ValueType &;
@@ -38,16 +42,10 @@ class EnumeratedArray {
3842
using const_pointer = const ValueType *;
3943

4044
EnumeratedArray() = default;
41-
EnumeratedArray(ValueType V) {
42-
for (IndexType IX = 0; IX < Size; ++IX) {
43-
Underlying[IX] = V;
44-
}
45-
}
45+
EnumeratedArray(ValueType V) { Underlying.fill(V); }
4646
EnumeratedArray(std::initializer_list<ValueType> Init) {
4747
assert(Init.size() == Size && "Incorrect initializer size");
48-
for (IndexType IX = 0; IX < Size; ++IX) {
49-
Underlying[IX] = *(Init.begin() + IX);
50-
}
48+
llvm::copy(Init, Underlying.begin());
5149
}
5250

5351
const ValueType &operator[](Enumeration Index) const {
@@ -62,23 +60,15 @@ class EnumeratedArray {
6260
IndexType size() const { return Size; }
6361
bool empty() const { return size() == 0; }
6462

65-
iterator begin() { return Underlying; }
66-
const_iterator begin() const { return Underlying; }
67-
68-
iterator end() { return begin() + size(); }
69-
const_iterator end() const { return begin() + size(); }
70-
71-
reverse_iterator rbegin() { return reverse_iterator(end()); }
72-
const_reverse_iterator rbegin() const {
73-
return const_reverse_iterator(end());
74-
}
75-
reverse_iterator rend() { return reverse_iterator(begin()); }
76-
const_reverse_iterator rend() const {
77-
return const_reverse_iterator(begin());
78-
}
63+
iterator begin() { return Underlying.begin(); }
64+
const_iterator begin() const { return Underlying.begin(); }
65+
iterator end() { return Underlying.end(); }
66+
const_iterator end() const { return Underlying.end(); }
7967

80-
private:
81-
ValueType Underlying[Size];
68+
reverse_iterator rbegin() { return Underlying.rbegin(); }
69+
const_reverse_iterator rbegin() const { return Underlying.rbegin(); }
70+
reverse_iterator rend() { return Underlying.rend(); }
71+
const_reverse_iterator rend() const { return Underlying.rend(); }
8272
};
8373

8474
} // namespace llvm

0 commit comments

Comments
 (0)