diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2025-04-05 01:46:13 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-05 01:46:13 -0400 |
| commit | 0cd31fc7ff157551cfbba5da35cd79f118d2a2e3 (patch) | |
| tree | a308488f5d85184c8ec402fb3f55f1cf2704443e /lib/libcxx/libc/src/__support/CPP/array.h | |
| parent | 8acedfd5baabab705946ad097746f9183ef62420 (diff) | |
| parent | cefe65c1b8abe65a22d4b68410db1be264fdeda0 (diff) | |
| download | zig-0cd31fc7ff157551cfbba5da35cd79f118d2a2e3.tar.gz zig-0cd31fc7ff157551cfbba5da35cd79f118d2a2e3.zip | |
Merge pull request #22780 from ziglang/llvm20
LLVM 20
Diffstat (limited to 'lib/libcxx/libc/src/__support/CPP/array.h')
| -rw-r--r-- | lib/libcxx/libc/src/__support/CPP/array.h | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/lib/libcxx/libc/src/__support/CPP/array.h b/lib/libcxx/libc/src/__support/CPP/array.h new file mode 100644 index 0000000000..db0a986b71 --- /dev/null +++ b/lib/libcxx/libc/src/__support/CPP/array.h @@ -0,0 +1,80 @@ +//===-- A self contained equivalent of std::array ---------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_ARRAY_H +#define LLVM_LIBC_SRC___SUPPORT_CPP_ARRAY_H + +#include "src/__support/CPP/iterator.h" // reverse_iterator +#include "src/__support/macros/attributes.h" +#include "src/__support/macros/config.h" +#include <stddef.h> // For size_t. + +namespace LIBC_NAMESPACE_DECL { +namespace cpp { + +template <class T, size_t N> struct array { + static_assert(N != 0, + "Cannot create a LIBC_NAMESPACE::cpp::array of size 0."); + + T Data[N]; + using value_type = T; + using iterator = T *; + using const_iterator = const T *; + using reverse_iterator = cpp::reverse_iterator<iterator>; + using const_reverse_iterator = cpp::reverse_iterator<const_iterator>; + + LIBC_INLINE constexpr T *data() { return Data; } + LIBC_INLINE constexpr const T *data() const { return Data; } + + LIBC_INLINE constexpr T &front() { return Data[0]; } + LIBC_INLINE constexpr const T &front() const { return Data[0]; } + + LIBC_INLINE constexpr T &back() { return Data[N - 1]; } + LIBC_INLINE constexpr const T &back() const { return Data[N - 1]; } + + LIBC_INLINE constexpr T &operator[](size_t Index) { return Data[Index]; } + + LIBC_INLINE constexpr const T &operator[](size_t Index) const { + return Data[Index]; + } + + LIBC_INLINE constexpr size_t size() const { return N; } + + LIBC_INLINE constexpr bool empty() const { return N == 0; } + + LIBC_INLINE constexpr iterator begin() { return Data; } + LIBC_INLINE constexpr const_iterator begin() const { return Data; } + LIBC_INLINE constexpr const_iterator cbegin() const { return begin(); } + + LIBC_INLINE constexpr iterator end() { return Data + N; } + LIBC_INLINE constexpr const_iterator end() const { return Data + N; } + LIBC_INLINE constexpr const_iterator cend() const { return end(); } + + LIBC_INLINE constexpr reverse_iterator rbegin() { + return reverse_iterator{end()}; + } + LIBC_INLINE constexpr const_reverse_iterator rbegin() const { + return const_reverse_iterator{end()}; + } + LIBC_INLINE constexpr const_reverse_iterator crbegin() const { + return rbegin(); + } + + LIBC_INLINE constexpr reverse_iterator rend() { + return reverse_iterator{begin()}; + } + LIBC_INLINE constexpr const_reverse_iterator rend() const { + return const_reverse_iterator{begin()}; + } + LIBC_INLINE constexpr const_reverse_iterator crend() const { return rend(); } +}; + +} // namespace cpp +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_CPP_ARRAY_H |
