-
Notifications
You must be signed in to change notification settings - Fork 15k
[ADT] Implement EnumeratedArray with std::array (NFC) #158407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[ADT] Implement EnumeratedArray with std::array (NFC) #158407
Conversation
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.
@llvm/pr-subscribers-llvm-adt Author: Kazu Hirata (kazutakahirata) ChangesEnumeratedArray provides an std::array-like interface except that you 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 Full diff: https://github.com/llvm/llvm-project/pull/158407.diff 1 Files Affected:
diff --git a/llvm/include/llvm/ADT/EnumeratedArray.h b/llvm/include/llvm/ADT/EnumeratedArray.h
index 93e1327306175..de150dfa3c3c2 100644
--- a/llvm/include/llvm/ADT/EnumeratedArray.h
+++ b/llvm/include/llvm/ADT/EnumeratedArray.h
@@ -15,8 +15,8 @@
#ifndef LLVM_ADT_ENUMERATEDARRAY_H
#define LLVM_ADT_ENUMERATEDARRAY_H
+#include <array>
#include <cassert>
-#include <iterator>
namespace llvm {
@@ -24,12 +24,14 @@ template <typename ValueType, typename Enumeration,
Enumeration LargestEnum = Enumeration::Last, typename IndexType = int,
IndexType Size = 1 + static_cast<IndexType>(LargestEnum)>
class EnumeratedArray {
-public:
- using iterator = ValueType *;
- using const_iterator = const ValueType *;
+ using ArrayTy = std::array<ValueType, Size>;
+ ArrayTy Underlying;
- using const_reverse_iterator = std::reverse_iterator<const_iterator>;
- using reverse_iterator = std::reverse_iterator<iterator>;
+public:
+ using iterator = typename ArrayTy::iterator;
+ using const_iterator = typename ArrayTy::const_iterator;
+ using reverse_iterator = typename ArrayTy::reverse_iterator;
+ using const_reverse_iterator = typename ArrayTy::const_reverse_iterator;
using value_type = ValueType;
using reference = ValueType &;
@@ -38,11 +40,7 @@ class EnumeratedArray {
using const_pointer = const ValueType *;
EnumeratedArray() = default;
- EnumeratedArray(ValueType V) {
- for (IndexType IX = 0; IX < Size; ++IX) {
- Underlying[IX] = V;
- }
- }
+ EnumeratedArray(ValueType V) { Underlying.fill(V); }
EnumeratedArray(std::initializer_list<ValueType> Init) {
assert(Init.size() == Size && "Incorrect initializer size");
for (IndexType IX = 0; IX < Size; ++IX) {
@@ -62,23 +60,15 @@ class EnumeratedArray {
IndexType size() const { return Size; }
bool empty() const { return size() == 0; }
- iterator begin() { return Underlying; }
- const_iterator begin() const { return Underlying; }
-
- iterator end() { return begin() + size(); }
- const_iterator end() const { return begin() + size(); }
-
- reverse_iterator rbegin() { return reverse_iterator(end()); }
- const_reverse_iterator rbegin() const {
- return const_reverse_iterator(end());
- }
- reverse_iterator rend() { return reverse_iterator(begin()); }
- const_reverse_iterator rend() const {
- return const_reverse_iterator(begin());
- }
+ iterator begin() { return Underlying.begin(); }
+ const_iterator begin() const { return Underlying.begin(); }
+ iterator end() { return Underlying.end(); }
+ const_iterator end() const { return Underlying.end(); }
-private:
- ValueType Underlying[Size];
+ reverse_iterator rbegin() { return Underlying.rbegin(); }
+ const_reverse_iterator rbegin() const { return Underlying.rbegin(); }
+ reverse_iterator rend() { return Underlying.rend(); }
+ const_reverse_iterator rend() const { return Underlying.rend(); }
};
} // namespace llvm
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. I think the side-effect is that this now allows for 0-sized arrays. Could we add a test for this?
Underlying[IX] = V; | ||
} | ||
} | ||
EnumeratedArray(ValueType V) { Underlying.fill(V); } | ||
EnumeratedArray(std::initializer_list<ValueType> Init) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be a llvm::copy
, no?
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.