| 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 "llvm/ADT/STLExtras.h" |
| 19 | #include <array> |
| 20 | #include <cassert> |
| 21 | |
| 22 | namespace llvm { |
| 23 | |
| 24 | template <typename ValueType, typename Enumeration, |
| 25 | Enumeration LargestEnum = Enumeration::Last, typename IndexType = int, |
| 26 | IndexType Size = 1 + static_cast<IndexType>(LargestEnum)> |
| 27 | class EnumeratedArray { |
| 28 | static_assert(Size > 0); |
| 29 | using ArrayTy = std::array<ValueType, Size>; |
| 30 | ArrayTy Underlying; |
| 31 | |
| 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; |
| 37 | |
| 38 | using value_type = ValueType; |
| 39 | using reference = ValueType &; |
| 40 | using const_reference = const ValueType &; |
| 41 | using pointer = ValueType *; |
| 42 | using const_pointer = const ValueType *; |
| 43 | |
| 44 | EnumeratedArray() = default; |
| 45 | EnumeratedArray(ValueType V) { Underlying.fill(V); } |
| 46 | EnumeratedArray(std::initializer_list<ValueType> Init) { |
| 47 | assert(Init.size() == Size && "Incorrect initializer size" ); |
| 48 | llvm::copy(Init, Underlying.begin()); |
| 49 | } |
| 50 | |
| 51 | const ValueType &operator[](Enumeration Index) const { |
| 52 | auto IX = static_cast<IndexType>(Index); |
| 53 | assert(IX >= 0 && IX < Size && "Index is out of bounds." ); |
| 54 | return Underlying[IX]; |
| 55 | } |
| 56 | ValueType &operator[](Enumeration Index) { |
| 57 | return const_cast<ValueType &>( |
| 58 | static_cast<const EnumeratedArray &>(*this)[Index]); |
| 59 | } |
| 60 | IndexType size() const { return Size; } |
| 61 | bool empty() const { return size() == 0; } |
| 62 | |
| 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(); } |
| 67 | |
| 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(); } |
| 72 | }; |
| 73 | |
| 74 | } // namespace llvm |
| 75 | |
| 76 | #endif // LLVM_ADT_ENUMERATEDARRAY_H |
| 77 | |