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//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_ARRAY_H
10#define LLVM_LIBC_SRC___SUPPORT_CPP_ARRAY_H
11
12#include "src/__support/CPP/iterator.h" // reverse_iterator
13#include "src/__support/macros/attributes.h"
14#include <stddef.h> // For size_t.
15
16namespace LIBC_NAMESPACE {
17namespace cpp {
18
19template <class T, size_t N> struct array {
20 static_assert(N != 0,
21 "Cannot create a LIBC_NAMESPACE::cpp::array of size 0.");
22
23 T Data[N];
24 using value_type = T;
25 using iterator = T *;
26 using const_iterator = const T *;
27 using reverse_iterator = cpp::reverse_iterator<iterator>;
28 using const_reverse_iterator = cpp::reverse_iterator<const_iterator>;
29
30 LIBC_INLINE constexpr T *data() { return Data; }
31 LIBC_INLINE constexpr const T *data() const { return Data; }
32
33 LIBC_INLINE constexpr T &front() { return Data[0]; }
34 LIBC_INLINE constexpr const T &front() const { return Data[0]; }
35
36 LIBC_INLINE constexpr T &back() { return Data[N - 1]; }
37 LIBC_INLINE constexpr const T &back() const { return Data[N - 1]; }
38
39 LIBC_INLINE constexpr T &operator[](size_t Index) { return Data[Index]; }
40
41 LIBC_INLINE constexpr const T &operator[](size_t Index) const {
42 return Data[Index];
43 }
44
45 LIBC_INLINE constexpr size_t size() const { return N; }
46
47 LIBC_INLINE constexpr bool empty() const { return N == 0; }
48
49 LIBC_INLINE constexpr iterator begin() { return Data; }
50 LIBC_INLINE constexpr const_iterator begin() const { return Data; }
51 LIBC_INLINE constexpr const_iterator cbegin() const { return begin(); }
52
53 LIBC_INLINE constexpr iterator end() { return Data + N; }
54 LIBC_INLINE constexpr const_iterator end() const { return Data + N; }
55 LIBC_INLINE constexpr const_iterator cend() const { return end(); }
56
57 LIBC_INLINE constexpr reverse_iterator rbegin() {
58 return reverse_iterator{end()};
59 }
60 LIBC_INLINE constexpr const_reverse_iterator rbegin() const {
61 return const_reverse_iterator{end()};
62 }
63 LIBC_INLINE constexpr const_reverse_iterator crbegin() const {
64 return rbegin();
65 }
66
67 LIBC_INLINE constexpr reverse_iterator rend() {
68 return reverse_iterator{begin()};
69 }
70 LIBC_INLINE constexpr const_reverse_iterator rend() const {
71 return const_reverse_iterator{begin()};
72 }
73 LIBC_INLINE constexpr const_reverse_iterator crend() const { return rend(); }
74};
75
76} // namespace cpp
77} // namespace LIBC_NAMESPACE
78
79#endif // LLVM_LIBC_SRC___SUPPORT_CPP_ARRAY_H
80

source code of libc/src/__support/CPP/array.h