[NFC][Py Reformat] Added more commits to .git-blame-ignore-revs
[llvm-project.git] / libc / src / __support / CPP / array.h
blobc0bd32f6bcbf55acbc23ff23a9f48a25c8a93993
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 //===----------------------------------------------------------------------===//
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 {
15 namespace cpp {
17 template <class T, size_t N> struct array {
18 static_assert(N != 0, "Cannot create a __llvm_libc::cpp::array of size 0.");
20 T Data[N];
21 using value_type = T;
22 using iterator = T *;
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; }
49 } // namespace cpp
50 } // namespace __llvm_libc
52 #endif // LLVM_LIBC_SRC_SUPPORT_CPP_ARRAY_H