File: ArrayRef.h

package info (click to toggle)
llvm-toolchain-13 1%3A13.0.1-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,418,840 kB
  • sloc: cpp: 5,290,826; ansic: 996,570; asm: 544,593; python: 188,212; objc: 72,027; lisp: 30,291; f90: 25,395; sh: 24,898; javascript: 9,780; pascal: 9,398; perl: 7,484; ml: 5,432; awk: 3,523; makefile: 2,913; xml: 953; cs: 573; fortran: 539
file content (139 lines) | stat: -rw-r--r-- 4,381 bytes parent folder | download | duplicates (3)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//===-- Self contained ArrayRef type ----------------------------*- 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
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_UTILS_CPP_ARRAYREF_H
#define LLVM_LIBC_UTILS_CPP_ARRAYREF_H

#include "Array.h"
#include "TypeTraits.h" //RemoveCVType

#include <stddef.h> // For size_t.

namespace __llvm_libc {
namespace cpp {

// The implementations of ArrayRef and MutableArrayRef in this file are based
// on the implementations of the types with the same names in
// llvm/ADT/ArrayRef.h. The implementations in this file are of a limited
// functionality, but can be extended in an as needed basis.
namespace internal {
template <typename QualifiedT> class ArrayRefBase {
public:
  using value_type = RemoveCVType<QualifiedT>;
  using pointer = value_type *;
  using const_pointer = const value_type *;
  using reference = value_type &;
  using const_reference = const value_type &;
  using iterator = const_pointer;
  using const_iterator = const_pointer;
  using size_type = size_t;
  using difference_type = ptrdiff_t;

  ArrayRefBase() = default;

  // Construct an ArrayRefBase from a single element.
  explicit ArrayRefBase(QualifiedT &OneElt) : Data(&OneElt), Length(1) {}

  // Construct an ArrayRefBase from a pointer and length.
  ArrayRefBase(QualifiedT *Data, size_t Length) : Data(Data), Length(Length) {}

  // Construct an ArrayRefBase from a range.
  ArrayRefBase(QualifiedT *Begin, QualifiedT *End)
      : Data(Begin), Length(End - Begin) {}

  // Construct an ArrayRefBase from a C array.
  template <size_t N>
  constexpr ArrayRefBase(QualifiedT (&Arr)[N]) : Data(Arr), Length(N) {}

  QualifiedT *data() const { return Data; }
  size_t size() const { return Length; }

  auto begin() const { return data(); }
  auto end() const { return data() + size(); }

  bool empty() const { return size() == 0; }

  auto operator[](size_t Index) const { return data()[Index]; }

  // slice(n, m) - Chop off the first N elements of the array, and keep M
  // elements in the array.
  auto slice(size_t N, size_t M) const { return ArrayRefBase(data() + N, M); }
  // slice(n) - Chop off the first N elements of the array.
  auto slice(size_t N) const { return slice(N, size() - N); }

  // Drop the first \p N elements of the array.
  auto drop_front(size_t N = 1) const { return slice(N, size() - N); }

  // Drop the last \p N elements of the array.
  auto drop_back(size_t N = 1) const { return slice(0, size() - N); }

  // Return a copy of *this with only the first \p N elements.
  auto take_front(size_t N = 1) const {
    if (N >= size())
      return *this;
    return drop_back(size() - N);
  }

  // Return a copy of *this with only the last \p N elements.
  auto take_back(size_t N = 1) const {
    if (N >= size())
      return *this;
    return drop_front(size() - N);
  }

  // equals - Check for element-wise equality.
  bool equals(ArrayRefBase<QualifiedT> RHS) const {
    if (Length != RHS.Length)
      return false;
    auto First1 = begin();
    auto Last1 = end();
    auto First2 = RHS.begin();
    for (; First1 != Last1; ++First1, ++First2) {
      if (!(*First1 == *First2)) {
        return false;
      }
    }
    return true;
  }

private:
  QualifiedT *Data = nullptr;
  size_t Length = 0;
};
} // namespace internal

template <typename T> struct ArrayRef : public internal::ArrayRefBase<const T> {
private:
  static_assert(IsSameV<T, RemoveCVType<T>>,
                "ArrayRef must have a non-const, non-volatile value_type");
  using Impl = internal::ArrayRefBase<const T>;
  using Impl::Impl;

public:
  // From Array.
  template <size_t N> ArrayRef(const Array<T, N> &Arr) : Impl(Arr.Data, N) {}
};

template <typename T>
struct MutableArrayRef : public internal::ArrayRefBase<T> {
private:
  static_assert(
      IsSameV<T, RemoveCVType<T>>,
      "MutableArrayRef must have a non-const, non-volatile value_type");
  using Impl = internal::ArrayRefBase<T>;
  using Impl::Impl;

public:
  // From Array.
  template <size_t N> MutableArrayRef(Array<T, N> &Arr) : Impl(Arr.Data, N) {}
};

} // namespace cpp
} // namespace __llvm_libc

#endif // LLVM_LIBC_UTILS_CPP_ARRAYREF_H