LLVM  16.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 
21 namespace llvm {
22 
23 template <typename ValueType, typename Enumeration,
24  Enumeration LargestEnum = Enumeration::Last, typename IndexType = int,
25  IndexType Size = 1 + static_cast<IndexType>(LargestEnum)>
27 public:
28  using iterator = ValueType *;
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 
35  using reference = ValueType &;
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  }
78  return const_reverse_iterator(begin());
79  }
80 
81 private:
82  ValueType Underlying[Size];
83 };
84 
85 } // namespace llvm
86 
87 #endif // LLVM_ADT_ENUMERATEDARRAY_H
llvm::Check::Size
@ Size
Definition: FileCheck.h:77
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::EnumeratedArray::rend
const_reverse_iterator rend() const
Definition: EnumeratedArray.h:77
llvm::EnumeratedArray::end
const_iterator end() const
Definition: EnumeratedArray.h:70
llvm::EnumeratedArray::end
iterator end()
Definition: EnumeratedArray.h:69
llvm::sys::path::end
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:235
llvm::sys::path::begin
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:226
llvm::EnumeratedArray::begin
const_iterator begin() const
Definition: EnumeratedArray.h:67
llvm::EnumeratedArray::operator[]
ValueType & operator[](Enumeration Index)
Definition: EnumeratedArray.h:58
llvm::EnumeratedArray::rbegin
reverse_iterator rbegin()
Definition: EnumeratedArray.h:72
llvm::EnumeratedArray::EnumeratedArray
EnumeratedArray(std::initializer_list< ValueType > Init)
Definition: EnumeratedArray.h:46
llvm::EnumeratedArray::operator[]
const ValueType & operator[](Enumeration Index) const
Definition: EnumeratedArray.h:53
llvm::EnumeratedArray::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: EnumeratedArray.h:31
int
Clang compiles this i1 i64 store i64 i64 store i64 i64 store i64 i64 store i64 align Which gets codegen d xmm0 movaps rbp movaps rbp movaps rbp movaps rbp rbp rbp rbp rbp It would be better to have movq s of instead of the movaps s LLVM produces ret int
Definition: README.txt:536
llvm::EnumeratedArray
Definition: EnumeratedArray.h:26
llvm::dwarf::Index
Index
Definition: Dwarf.h:490
llvm::EnumeratedArray::size
IndexType size() const
Definition: EnumeratedArray.h:63
Index
uint32_t Index
Definition: ELFObjHandler.cpp:83
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::EnumeratedArray::rbegin
const_reverse_iterator rbegin() const
Definition: EnumeratedArray.h:73
llvm::EnumeratedArray::empty
bool empty() const
Definition: EnumeratedArray.h:64
llvm::size
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:1715
llvm::EnumeratedArray::rend
reverse_iterator rend()
Definition: EnumeratedArray.h:76
llvm::ValueType
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
Definition: ScheduleDAGInstrs.h:107
llvm::Init
Definition: Record.h:282
llvm::EnumeratedArray::begin
iterator begin()
Definition: EnumeratedArray.h:66
llvm::PointerUnion< const Value *, const PseudoSourceValue * >
llvm::EnumeratedArray::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Definition: EnumeratedArray.h:32
llvm::EnumeratedArray::EnumeratedArray
EnumeratedArray(ValueType V)
Definition: EnumeratedArray.h:41