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 "llvm/ADT/STLExtras.h"
19#include <array>
20#include <cassert>
21
22namespace llvm {
23
24template <typename ValueType, typename Enumeration,
25 Enumeration LargestEnum = Enumeration::Last, typename IndexType = int,
26 IndexType Size = 1 + static_cast<IndexType>(LargestEnum)>
28 static_assert(Size > 0);
29 using ArrayTy = std::array<ValueType, Size>;
30 ArrayTy Underlying;
31
32public:
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
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
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains some templates that are useful if you are working with the STL at all.
EnumeratedArray(std::initializer_list< ValueType > Init)
typename ArrayTy::const_iterator const_iterator
const_iterator begin() const
const ValueType * const_pointer
const_reverse_iterator rbegin() const
const ValueType & const_reference
typename ArrayTy::const_reverse_iterator const_reverse_iterator
EnumeratedArray(ValueType V)
const ValueType & operator[](Enumeration Index) const
const_iterator end() const
ValueType & operator[](Enumeration Index)
reverse_iterator rend()
typename ArrayTy::iterator iterator
const_reverse_iterator rend() const
IndexType size() const
reverse_iterator rbegin()
typename ArrayTy::reverse_iterator reverse_iterator
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:1657
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1815
PointerUnion< const Value *, const PseudoSourceValue * > ValueType