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 <stddef.h> // For size_t.
14 namespace __llvm_libc
{
17 template <class T
, size_t N
> struct array
{
18 static_assert(N
!= 0, "Cannot create a __llvm_libc::cpp::array of size 0.");
23 using const_iterator
= const T
*;
25 constexpr T
*data() { return Data
; }
26 constexpr const T
*data() const { return Data
; }
28 constexpr T
&front() { return Data
[0]; }
29 constexpr T
&front() const { return Data
[0]; }
31 constexpr T
&back() { return Data
[N
- 1]; }
32 constexpr T
&back() const { return Data
[N
- 1]; }
34 constexpr T
&operator[](size_t Index
) { return Data
[Index
]; }
36 constexpr const T
&operator[](size_t Index
) const { return Data
[Index
]; }
38 constexpr size_t size() const { return N
; }
40 constexpr bool empty() const { return N
== 0; }
42 constexpr iterator
begin() { return Data
; }
43 constexpr const_iterator
begin() const { return Data
; }
45 constexpr iterator
end() { return Data
+ N
; }
46 const_iterator
end() const { return Data
+ N
; }
50 } // namespace __llvm_libc
52 #endif // LLVM_LIBC_SRC_SUPPORT_CPP_ARRAY_H