1 //===-- A self contained equivalent of std::array ---------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC___SUPPORT_CPP_ARRAY_H
10 #define LLVM_LIBC_SRC___SUPPORT_CPP_ARRAY_H
12 #include "src/__support/CPP/iterator.h" // reverse_iterator
13 #include "src/__support/macros/attributes.h"
14 #include "src/__support/macros/config.h"
15 #include <stddef.h> // For size_t.
17 namespace LIBC_NAMESPACE_DECL
{
20 template <class T
, size_t N
> struct array
{
22 "Cannot create a LIBC_NAMESPACE::cpp::array of size 0.");
27 using const_iterator
= const T
*;
28 using reverse_iterator
= cpp::reverse_iterator
<iterator
>;
29 using const_reverse_iterator
= cpp::reverse_iterator
<const_iterator
>;
31 LIBC_INLINE
constexpr T
*data() { return Data
; }
32 LIBC_INLINE
constexpr const T
*data() const { return Data
; }
34 LIBC_INLINE
constexpr T
&front() { return Data
[0]; }
35 LIBC_INLINE
constexpr const T
&front() const { return Data
[0]; }
37 LIBC_INLINE
constexpr T
&back() { return Data
[N
- 1]; }
38 LIBC_INLINE
constexpr const T
&back() const { return Data
[N
- 1]; }
40 LIBC_INLINE
constexpr T
&operator[](size_t Index
) { return Data
[Index
]; }
42 LIBC_INLINE
constexpr const T
&operator[](size_t Index
) const {
46 LIBC_INLINE
constexpr size_t size() const { return N
; }
48 LIBC_INLINE
constexpr bool empty() const { return N
== 0; }
50 LIBC_INLINE
constexpr iterator
begin() { return Data
; }
51 LIBC_INLINE
constexpr const_iterator
begin() const { return Data
; }
52 LIBC_INLINE
constexpr const_iterator
cbegin() const { return begin(); }
54 LIBC_INLINE
constexpr iterator
end() { return Data
+ N
; }
55 LIBC_INLINE
constexpr const_iterator
end() const { return Data
+ N
; }
56 LIBC_INLINE
constexpr const_iterator
cend() const { return end(); }
58 LIBC_INLINE
constexpr reverse_iterator
rbegin() {
59 return reverse_iterator
{end()};
61 LIBC_INLINE
constexpr const_reverse_iterator
rbegin() const {
62 return const_reverse_iterator
{end()};
64 LIBC_INLINE
constexpr const_reverse_iterator
crbegin() const {
68 LIBC_INLINE
constexpr reverse_iterator
rend() {
69 return reverse_iterator
{begin()};
71 LIBC_INLINE
constexpr const_reverse_iterator
rend() const {
72 return const_reverse_iterator
{begin()};
74 LIBC_INLINE
constexpr const_reverse_iterator
crend() const { return rend(); }
78 } // namespace LIBC_NAMESPACE_DECL
80 #endif // LLVM_LIBC_SRC___SUPPORT_CPP_ARRAY_H