blob: 8a26e69bb330eee8af794e3d6b3ae6f0639fcc12 [file] [log] [blame]
Wyatt Hepler0412a7d2020-01-28 16:27:32 -08001// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <iterator>
17
Wyatt Hepler023f35b2020-07-01 09:40:50 -070018#include "pw_polyfill/standard_library/namespace.h"
19
20_PW_POLYFILL_BEGIN_NAMESPACE_STD
Wyatt Hepler0412a7d2020-01-28 16:27:32 -080021
22template <typename T, decltype(sizeof(0)) kSize>
23struct array {
24 using value_type = T;
25 using size_type = decltype(kSize);
26 using difference_type =
27 decltype(static_cast<int*>(nullptr) - static_cast<int*>(nullptr));
28 using reference = value_type&;
29 using const_reference = const value_type&;
30 using pointer = value_type*;
31 using const_pointer = const value_type*;
32 using iterator = T*;
33 using const_iterator = const T*;
34 using reverse_iterator = std::reverse_iterator<iterator>;
35 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
36
37 // NOT IMPLEMENTED: at() does not bounds checking.
38 constexpr reference at(size_type index) { return data()[index]; }
39 constexpr const_reference at(size_type index) const { return data()[index]; }
40
41 constexpr reference operator[](size_type index) { return data()[index]; }
42 constexpr const_reference operator[](size_type index) const {
43 return data()[index];
44 }
45
46 constexpr reference front() { return data()[0]; }
47 constexpr const_reference front() const { return data()[0]; }
48
49 constexpr reference back() {
50 static_assert(kSize > 0);
51 return data()[size() - 1];
52 }
53 constexpr const_reference back() const {
54 static_assert(kSize > 0);
55 return data()[size() - 1];
56 }
57
58 constexpr pointer data() noexcept {
59 static_assert(kSize > 0);
60 return __data;
61 }
62 constexpr const_pointer data() const noexcept {
63 static_assert(kSize > 0);
64 return __data;
65 }
66
67 constexpr iterator begin() noexcept { return data(); }
68 constexpr const_iterator begin() const noexcept { return data(); }
69 constexpr const_iterator cbegin() const noexcept { return begin(); }
70
71 constexpr iterator end() noexcept { return data() + kSize; }
72 constexpr const_iterator end() const noexcept { return data() + kSize; }
73 constexpr const_iterator cend() const noexcept { return end(); }
74
75 // NOT IMPLEMENTED
76 constexpr reverse_iterator rbegin() noexcept;
77 constexpr const_reverse_iterator rbegin() const noexcept;
78 constexpr const_reverse_iterator crbegin() const noexcept;
79
80 // NOT IMPLEMENTED
81 constexpr reverse_iterator rend() noexcept;
82 constexpr const_reverse_iterator rend() const noexcept;
83 constexpr const_reverse_iterator crend() const noexcept;
84
85 [[nodiscard]] constexpr bool empty() const noexcept { return kSize == 0u; }
86
87 constexpr size_type size() const noexcept { return kSize; }
88
89 constexpr size_type max_size() const noexcept { return size(); }
90
91 constexpr void fill(const T& value) {
92 for (T& array_value : __data) {
93 array_value = value;
94 }
95 }
96
97 // NOT IMPLEMENTED
98 constexpr void swap(array& other) noexcept;
99
100 T __data[kSize];
101};
102
103// NOT IMPLEMENTED: comparison operators, get, swap, tuple specializations
104
Wyatt Hepler023f35b2020-07-01 09:40:50 -0700105_PW_POLYFILL_END_NAMESPACE_STD