LLVM 19.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<ValueType, Enumeration, LargestEnum,
61 IndexType, Size> &>(*this)[Index]);
62 }
63 IndexType size() const { return Size; }
64 bool empty() const { return size() == 0; }
65
66 iterator begin() { return Underlying; }
67 const_iterator begin() const { return Underlying; }
68
69 iterator end() { return begin() + size(); }
70 const_iterator end() const { return begin() + size(); }
71
74 return const_reverse_iterator(end());
75 }
76 reverse_iterator rend() { return reverse_iterator(begin()); }
78 return const_reverse_iterator(begin());
79 }
80
81private:
82 ValueType Underlying[Size];
83};
84
85} // namespace llvm
86
87#endif // LLVM_ADT_ENUMERATEDARRAY_H
uint64_t Size
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
EnumeratedArray(std::initializer_list< ValueType > Init)
const_iterator begin() const
const_reverse_iterator rbegin() const
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
IndexType size() const
reverse_iterator rbegin()
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:1689
PointerUnion< const Value *, const PseudoSourceValue * > ValueType