xref: /aosp_15_r20/external/llvm-libc/src/__support/CPP/array.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
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 "src/__support/macros/config.h"
15 #include <stddef.h> // For size_t.
16 
17 namespace LIBC_NAMESPACE_DECL {
18 namespace cpp {
19 
20 template <class T, size_t N> struct array {
21   static_assert(N != 0,
22                 "Cannot create a LIBC_NAMESPACE::cpp::array of size 0.");
23 
24   T Data[N];
25   using value_type = T;
26   using iterator = T *;
27   using const_iterator = const T *;
28   using reverse_iterator = cpp::reverse_iterator<iterator>;
29   using const_reverse_iterator = cpp::reverse_iterator<const_iterator>;
30 
dataarray31   LIBC_INLINE constexpr T *data() { return Data; }
dataarray32   LIBC_INLINE constexpr const T *data() const { return Data; }
33 
frontarray34   LIBC_INLINE constexpr T &front() { return Data[0]; }
frontarray35   LIBC_INLINE constexpr const T &front() const { return Data[0]; }
36 
backarray37   LIBC_INLINE constexpr T &back() { return Data[N - 1]; }
backarray38   LIBC_INLINE constexpr const T &back() const { return Data[N - 1]; }
39 
40   LIBC_INLINE constexpr T &operator[](size_t Index) { return Data[Index]; }
41 
42   LIBC_INLINE constexpr const T &operator[](size_t Index) const {
43     return Data[Index];
44   }
45 
sizearray46   LIBC_INLINE constexpr size_t size() const { return N; }
47 
emptyarray48   LIBC_INLINE constexpr bool empty() const { return N == 0; }
49 
beginarray50   LIBC_INLINE constexpr iterator begin() { return Data; }
beginarray51   LIBC_INLINE constexpr const_iterator begin() const { return Data; }
cbeginarray52   LIBC_INLINE constexpr const_iterator cbegin() const { return begin(); }
53 
endarray54   LIBC_INLINE constexpr iterator end() { return Data + N; }
endarray55   LIBC_INLINE constexpr const_iterator end() const { return Data + N; }
cendarray56   LIBC_INLINE constexpr const_iterator cend() const { return end(); }
57 
rbeginarray58   LIBC_INLINE constexpr reverse_iterator rbegin() {
59     return reverse_iterator{end()};
60   }
rbeginarray61   LIBC_INLINE constexpr const_reverse_iterator rbegin() const {
62     return const_reverse_iterator{end()};
63   }
crbeginarray64   LIBC_INLINE constexpr const_reverse_iterator crbegin() const {
65     return rbegin();
66   }
67 
rendarray68   LIBC_INLINE constexpr reverse_iterator rend() {
69     return reverse_iterator{begin()};
70   }
rendarray71   LIBC_INLINE constexpr const_reverse_iterator rend() const {
72     return const_reverse_iterator{begin()};
73   }
crendarray74   LIBC_INLINE constexpr const_reverse_iterator crend() const { return rend(); }
75 };
76 
77 } // namespace cpp
78 } // namespace LIBC_NAMESPACE_DECL
79 
80 #endif // LLVM_LIBC_SRC___SUPPORT_CPP_ARRAY_H
81