• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_UTILS_CPP_ARRAY_H
10 #define LLVM_LIBC_UTILS_CPP_ARRAY_H
11 
12 #include <stddef.h> // For size_t.
13 
14 namespace __llvm_libc {
15 namespace cpp {
16 
17 template <class T, size_t N> struct Array {
18   static_assert(N != 0, "Cannot create a __llvm_libc::cpp::Array of size 0.");
19 
20   T Data[N];
21 
22   using iterator = T *;
23   using const_iterator = const T *;
24 
dataArray25   constexpr T *data() { return Data; }
dataArray26   constexpr const T *data() const { return Data; }
27 
frontArray28   constexpr T &front() { return Data[0]; }
frontArray29   constexpr T &front() const { return Data[0]; }
30 
backArray31   constexpr T &back() { return Data[N - 1]; }
backArray32   constexpr T &back() const { return Data[N - 1]; }
33 
34   constexpr T &operator[](size_t Index) { return Data[Index]; }
35 
36   constexpr const T &operator[](size_t Index) const { return Data[Index]; }
37 
sizeArray38   constexpr size_t size() const { return N; }
39 
emptyArray40   constexpr bool empty() const { return N == 0; }
41 
beginArray42   constexpr iterator begin() { return Data; }
beginArray43   constexpr const_iterator begin() const { return Data; }
44 
endArray45   constexpr iterator end() { return Data + N; }
endArray46   const_iterator end() const { return Data + N; }
47 };
48 
49 } // namespace cpp
50 } // namespace __llvm_libc
51 
52 #endif // LLVM_LIBC_UTILS_CPP_ARRAY_H
53