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 16 namespace LIBC_NAMESPACE { 17 namespace cpp { 18 19 template <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 dataarray30 LIBC_INLINE constexpr T *data() { return Data; } dataarray31 LIBC_INLINE constexpr const T *data() const { return Data; } 32 frontarray33 LIBC_INLINE constexpr T &front() { return Data[0]; } frontarray34 LIBC_INLINE constexpr const T &front() const { return Data[0]; } 35 backarray36 LIBC_INLINE constexpr T &back() { return Data[N - 1]; } backarray37 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 sizearray45 LIBC_INLINE constexpr size_t size() const { return N; } 46 emptyarray47 LIBC_INLINE constexpr bool empty() const { return N == 0; } 48 beginarray49 LIBC_INLINE constexpr iterator begin() { return Data; } beginarray50 LIBC_INLINE constexpr const_iterator begin() const { return Data; } cbeginarray51 LIBC_INLINE constexpr const_iterator cbegin() const { return begin(); } 52 endarray53 LIBC_INLINE constexpr iterator end() { return Data + N; } endarray54 LIBC_INLINE constexpr const_iterator end() const { return Data + N; } cendarray55 LIBC_INLINE constexpr const_iterator cend() const { return end(); } 56 rbeginarray57 LIBC_INLINE constexpr reverse_iterator rbegin() { 58 return reverse_iterator{end()}; 59 } rbeginarray60 LIBC_INLINE constexpr const_reverse_iterator rbegin() const { 61 return const_reverse_iterator{end()}; 62 } crbeginarray63 LIBC_INLINE constexpr const_reverse_iterator crbegin() const { 64 return rbegin(); 65 } 66 rendarray67 LIBC_INLINE constexpr reverse_iterator rend() { 68 return reverse_iterator{begin()}; 69 } rendarray70 LIBC_INLINE constexpr const_reverse_iterator rend() const { 71 return const_reverse_iterator{begin()}; 72 } crendarray73 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