EnumeratedArray.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/ADT/EnumeratedArray.h - Enumerated Array-------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file defines an array type that can be indexed using scoped enum
  16. /// values.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_ADT_ENUMERATEDARRAY_H
  20. #define LLVM_ADT_ENUMERATEDARRAY_H
  21. #include <cassert>
  22. #include <iterator>
  23. namespace llvm {
  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. public:
  29. using iterator = ValueType *;
  30. using const_iterator = const ValueType *;
  31. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  32. using reverse_iterator = std::reverse_iterator<iterator>;
  33. using value_type = ValueType;
  34. using reference = ValueType &;
  35. using const_reference = const ValueType &;
  36. using pointer = ValueType *;
  37. using const_pointer = const ValueType *;
  38. EnumeratedArray() = default;
  39. EnumeratedArray(ValueType V) {
  40. for (IndexType IX = 0; IX < Size; ++IX) {
  41. Underlying[IX] = V;
  42. }
  43. }
  44. EnumeratedArray(std::initializer_list<ValueType> Init) {
  45. assert(Init.size() == Size && "Incorrect initializer size");
  46. for (IndexType IX = 0; IX < Size; ++IX) {
  47. Underlying[IX] = *(Init.begin() + IX);
  48. }
  49. }
  50. const ValueType &operator[](Enumeration Index) const {
  51. auto IX = static_cast<IndexType>(Index);
  52. assert(IX >= 0 && IX < Size && "Index is out of bounds.");
  53. return Underlying[IX];
  54. }
  55. ValueType &operator[](Enumeration Index) {
  56. return const_cast<ValueType &>(
  57. static_cast<const EnumeratedArray<ValueType, Enumeration, LargestEnum,
  58. IndexType, Size> &>(*this)[Index]);
  59. }
  60. IndexType size() const { return Size; }
  61. bool empty() const { return size() == 0; }
  62. iterator begin() { return Underlying; }
  63. const_iterator begin() const { return Underlying; }
  64. iterator end() { return begin() + size(); }
  65. const_iterator end() const { return begin() + size(); }
  66. reverse_iterator rbegin() { return reverse_iterator(end()); }
  67. const_reverse_iterator rbegin() const {
  68. return const_reverse_iterator(end());
  69. }
  70. reverse_iterator rend() { return reverse_iterator(begin()); }
  71. const_reverse_iterator rend() const {
  72. return const_reverse_iterator(begin());
  73. }
  74. private:
  75. ValueType Underlying[Size];
  76. };
  77. } // namespace llvm
  78. #endif // LLVM_ADT_ENUMERATEDARRAY_H
  79. #ifdef __GNUC__
  80. #pragma GCC diagnostic pop
  81. #endif