LLVM 22.0.0git
EnumeratedArray.h
Go to the documentation of this file.
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#include <iterator>
20
21namespace llvm {
22
23template <typename ValueType, typename Enumeration,
24 Enumeration LargestEnum = Enumeration::Last, typename IndexType = int,
25 IndexType Size = 1 + static_cast<IndexType>(LargestEnum)>
27public:
29 using const_iterator = const ValueType *;
30
31 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
32 using reverse_iterator = std::reverse_iterator<iterator>;
33
36 using const_reference = const ValueType &;
37 using pointer = ValueType *;
38 using const_pointer = const ValueType *;
39
40 EnumeratedArray() = default;
42 for (IndexType IX = 0; IX < Size; ++IX) {
43 Underlying[IX] = V;
44 }
45 }
46 EnumeratedArray(std::initializer_list<ValueType> Init) {
47 assert(Init.size() == Size && "Incorrect initializer size");
48 for (IndexType IX = 0; IX < Size; ++IX) {
49 Underlying[IX] = *(Init.begin() + IX);
50 }
51 }
52
53 const ValueType &operator[](Enumeration Index) const {
54 auto IX = static_cast<IndexType>(Index);
55 assert(IX >= 0 && IX < Size && "Index is out of bounds.");
56 return Underlying[IX];
57 }
58 ValueType &operator[](Enumeration Index) {
59 return const_cast<ValueType &>(
60 static_cast<const EnumeratedArray &>(*this)[Index]);
61 }
62 IndexType size() const { return Size; }
63 bool empty() const { return size() == 0; }
64
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
79
80private:
81 ValueType Underlying[Size];
82};
83
84} // namespace llvm
85
86#endif // LLVM_ADT_ENUMERATEDARRAY_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
EnumeratedArray(std::initializer_list< ValueType > Init)
const_iterator begin() const
const ValueType * const_pointer
const_reverse_iterator rbegin() const
const ValueType & const_reference
std::reverse_iterator< iterator > reverse_iterator
EnumeratedArray(ValueType V)
const ValueType & operator[](Enumeration Index) const
const_iterator end() const
ValueType & operator[](Enumeration Index)
reverse_iterator rend()
std::reverse_iterator< const_iterator > const_reverse_iterator
const_reverse_iterator rend() const
const ValueType * const_iterator
IndexType size() const
reverse_iterator rbegin()
This is an optimization pass for GlobalISel generic memory operations.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1685
PointerUnion< const Value *, const PseudoSourceValue * > ValueType