Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:04

0001 //===- llvm/ADT/EnumeratedArray.h - Enumerated Array-------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 ///
0009 /// \file
0010 /// This file defines an array type that can be indexed using scoped enum
0011 /// values.
0012 ///
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_ADT_ENUMERATEDARRAY_H
0016 #define LLVM_ADT_ENUMERATEDARRAY_H
0017 
0018 #include <cassert>
0019 #include <iterator>
0020 
0021 namespace llvm {
0022 
0023 template <typename ValueType, typename Enumeration,
0024           Enumeration LargestEnum = Enumeration::Last, typename IndexType = int,
0025           IndexType Size = 1 + static_cast<IndexType>(LargestEnum)>
0026 class EnumeratedArray {
0027 public:
0028   using iterator = ValueType *;
0029   using const_iterator = const ValueType *;
0030 
0031   using const_reverse_iterator = std::reverse_iterator<const_iterator>;
0032   using reverse_iterator = std::reverse_iterator<iterator>;
0033 
0034   using value_type = ValueType;
0035   using reference = ValueType &;
0036   using const_reference = const ValueType &;
0037   using pointer = ValueType *;
0038   using const_pointer = const ValueType *;
0039 
0040   EnumeratedArray() = default;
0041   EnumeratedArray(ValueType V) {
0042     for (IndexType IX = 0; IX < Size; ++IX) {
0043       Underlying[IX] = V;
0044     }
0045   }
0046   EnumeratedArray(std::initializer_list<ValueType> Init) {
0047     assert(Init.size() == Size && "Incorrect initializer size");
0048     for (IndexType IX = 0; IX < Size; ++IX) {
0049       Underlying[IX] = *(Init.begin() + IX);
0050     }
0051   }
0052 
0053   const ValueType &operator[](Enumeration Index) const {
0054     auto IX = static_cast<IndexType>(Index);
0055     assert(IX >= 0 && IX < Size && "Index is out of bounds.");
0056     return Underlying[IX];
0057   }
0058   ValueType &operator[](Enumeration Index) {
0059     return const_cast<ValueType &>(
0060         static_cast<const EnumeratedArray<ValueType, Enumeration, LargestEnum,
0061                                           IndexType, Size> &>(*this)[Index]);
0062   }
0063   IndexType size() const { return Size; }
0064   bool empty() const { return size() == 0; }
0065 
0066   iterator begin() { return Underlying; }
0067   const_iterator begin() const { return Underlying; }
0068 
0069   iterator end() { return begin() + size(); }
0070   const_iterator end() const { return begin() + size(); }
0071 
0072   reverse_iterator rbegin() { return reverse_iterator(end()); }
0073   const_reverse_iterator rbegin() const {
0074     return const_reverse_iterator(end());
0075   }
0076   reverse_iterator rend() { return reverse_iterator(begin()); }
0077   const_reverse_iterator rend() const {
0078     return const_reverse_iterator(begin());
0079   }
0080 
0081 private:
0082   ValueType Underlying[Size];
0083 };
0084 
0085 } // namespace llvm
0086 
0087 #endif // LLVM_ADT_ENUMERATEDARRAY_H