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/macros/attributes.h"
13 #include <stddef.h> // For size_t.
15 namespace LIBC_NAMESPACE
{
18 template <class T
, size_t N
> struct array
{
20 "Cannot create a LIBC_NAMESPACE::cpp::array of size 0.");
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 {
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
; }
54 } // namespace LIBC_NAMESPACE
56 #endif // LLVM_LIBC_SRC___SUPPORT_CPP_ARRAY_H