Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / src / __support / CPP / array.h
blob12e574899988060fec360a96d282ed10c6c23e08
1 //===-- A self contained equivalent of std::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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC___SUPPORT_CPP_ARRAY_H
10 #define LLVM_LIBC_SRC___SUPPORT_CPP_ARRAY_H
12 #include "src/__support/macros/attributes.h"
13 #include <stddef.h> // For size_t.
15 namespace LIBC_NAMESPACE {
16 namespace cpp {
18 template <class T, size_t N> struct array {
19 static_assert(N != 0,
20 "Cannot create a LIBC_NAMESPACE::cpp::array of size 0.");
22 T Data[N];
23 using value_type = T;
24 using iterator = T *;
25 using const_iterator = const T *;
27 LIBC_INLINE constexpr T *data() { return Data; }
28 LIBC_INLINE constexpr const T *data() const { return Data; }
30 LIBC_INLINE constexpr T &front() { return Data[0]; }
31 LIBC_INLINE constexpr T &front() const { return Data[0]; }
33 LIBC_INLINE constexpr T &back() { return Data[N - 1]; }
34 LIBC_INLINE constexpr T &back() const { return Data[N - 1]; }
36 LIBC_INLINE constexpr T &operator[](size_t Index) { return Data[Index]; }
38 LIBC_INLINE constexpr const T &operator[](size_t Index) const {
39 return Data[Index];
42 LIBC_INLINE constexpr size_t size() const { return N; }
44 LIBC_INLINE constexpr bool empty() const { return N == 0; }
46 LIBC_INLINE constexpr iterator begin() { return Data; }
47 LIBC_INLINE constexpr const_iterator begin() const { return Data; }
49 LIBC_INLINE constexpr iterator end() { return Data + N; }
50 LIBC_INLINE const_iterator end() const { return Data + N; }
53 } // namespace cpp
54 } // namespace LIBC_NAMESPACE
56 #endif // LLVM_LIBC_SRC___SUPPORT_CPP_ARRAY_H