1 //===- llvm/ADT/EnumeratedArray.h - Enumerated Array-------------*- 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 /// \file
10 /// This file defines an array type that can be indexed using scoped enum
11 /// values.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_ADT_ENUMERATEDARRAY_H
16 #define LLVM_ADT_ENUMERATEDARRAY_H
17 
18 #include <cassert>
19 
20 namespace llvm {
21 
22 template <typename ValueType, typename Enumeration,
23           Enumeration LargestEnum = Enumeration::Last, typename IndexType = int,
24           IndexType Size = 1 + static_cast<IndexType>(LargestEnum)>
25 class EnumeratedArray {
26 public:
27   EnumeratedArray() = default;
28   EnumeratedArray(ValueType V) {
29     for (IndexType IX = 0; IX < Size; ++IX) {
30       Underlying[IX] = V;
31     }
32   }
33   inline const ValueType &operator[](const Enumeration Index) const {
34     auto IX = static_cast<const IndexType>(Index);
35     assert(IX >= 0 && IX < Size && "Index is out of bounds.");
36     return Underlying[IX];
37   }
38   inline ValueType &operator[](const Enumeration Index) {
39     return const_cast<ValueType &>(
40         static_cast<const EnumeratedArray<ValueType, Enumeration, LargestEnum,
41                                           IndexType, Size> &>(*this)[Index]);
42   }
43   inline IndexType size() { return Size; }
44 
45 private:
46   ValueType Underlying[Size];
47 };
48 
49 } // namespace llvm
50 
51 #endif // LLVM_ADT_ENUMERATEDARRAY_H
52