[llvm] 9b70b84 - [ADT] Implement EnumeratedArray with std::array (NFC) (#158407)

via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 13 13:50:37 PDT 2025


Author: Kazu Hirata
Date: 2025-09-13T13:50:33-07:00
New Revision: 9b70b84b89f0de2dc4641da02fe54a6a1ef46e6e

URL: https://github.com/llvm/llvm-project/commit/9b70b84b89f0de2dc4641da02fe54a6a1ef46e6e
DIFF: https://github.com/llvm/llvm-project/commit/9b70b84b89f0de2dc4641da02fe54a6a1ef46e6e.diff

LOG: [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.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/EnumeratedArray.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/EnumeratedArray.h b/llvm/include/llvm/ADT/EnumeratedArray.h
index 93e1327306175..2fe6be434f11b 100644
--- a/llvm/include/llvm/ADT/EnumeratedArray.h
+++ b/llvm/include/llvm/ADT/EnumeratedArray.h
@@ -15,8 +15,9 @@
 #ifndef LLVM_ADT_ENUMERATEDARRAY_H
 #define LLVM_ADT_ENUMERATEDARRAY_H
 
+#include "llvm/ADT/STLExtras.h"
+#include <array>
 #include <cassert>
-#include <iterator>
 
 namespace llvm {
 
@@ -24,12 +25,15 @@ 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 *;
+  static_assert(Size > 0);
+  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,16 +42,10 @@ 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) {
-      Underlying[IX] = *(Init.begin() + IX);
-    }
+    llvm::copy(Init, Underlying.begin());
   }
 
   const ValueType &operator[](Enumeration Index) const {
@@ -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


        


More information about the llvm-commits mailing list