aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm16/include/llvm/ADT/EnumeratedArray.h
blob: 3bb29640d0910b5b5d862be499186b06b590796c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#pragma once

#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif

//===- llvm/ADT/EnumeratedArray.h - Enumerated Array-------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file defines an array type that can be indexed using scoped enum
/// values.
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_ADT_ENUMERATEDARRAY_H
#define LLVM_ADT_ENUMERATEDARRAY_H

#include <cassert>
#include <iterator>

namespace llvm {

template <typename ValueType, typename Enumeration,
          Enumeration LargestEnum = Enumeration::Last, typename IndexType = int,
          IndexType Size = 1 + static_cast<IndexType>(LargestEnum)>
class EnumeratedArray {
public:
  using iterator = ValueType *;
  using const_iterator = const ValueType *;

  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  using reverse_iterator = std::reverse_iterator<iterator>;

  using value_type = ValueType;
  using reference = ValueType &;
  using const_reference = const ValueType &;
  using pointer = ValueType *;
  using const_pointer = const ValueType *;

  EnumeratedArray() = default;
  EnumeratedArray(ValueType V) {
    for (IndexType IX = 0; IX < Size; ++IX) {
      Underlying[IX] = V;
    }
  }
  EnumeratedArray(std::initializer_list<ValueType> Init) {
    assert(Init.size() == Size && "Incorrect initializer size");
    for (IndexType IX = 0; IX < Size; ++IX) {
      Underlying[IX] = *(Init.begin() + IX);
    }
  }

  const ValueType &operator[](Enumeration Index) const {
    auto IX = static_cast<IndexType>(Index);
    assert(IX >= 0 && IX < Size && "Index is out of bounds.");
    return Underlying[IX];
  }
  ValueType &operator[](Enumeration Index) {
    return const_cast<ValueType &>(
        static_cast<const EnumeratedArray<ValueType, Enumeration, LargestEnum,
                                          IndexType, Size> &>(*this)[Index]);
  }
  IndexType size() const { return Size; }
  bool empty() const { return size() == 0; }

  iterator begin() { return Underlying; }
  const_iterator begin() const { return Underlying; }

  iterator end() { return begin() + size(); }
  const_iterator end() const { return begin() + size(); }

  reverse_iterator rbegin() { return reverse_iterator(end()); }
  const_reverse_iterator rbegin() const {
    return const_reverse_iterator(end());
  }
  reverse_iterator rend() { return reverse_iterator(begin()); }
  const_reverse_iterator rend() const {
    return const_reverse_iterator(begin());
  }

private:
  ValueType Underlying[Size];
};

} // namespace llvm

#endif // LLVM_ADT_ENUMERATEDARRAY_H

#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif