| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2018 Jaroslav Jindrak
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | #ifndef LIBCPP_BITS_ADT_ARRAY
|
|---|
| 8 | #define LIBCPP_BITS_ADT_ARRAY
|
|---|
| 9 |
|
|---|
| 10 | #include <iterator>
|
|---|
| 11 | #include <utility>
|
|---|
| 12 |
|
|---|
| 13 | namespace std
|
|---|
| 14 | {
|
|---|
| 15 | /**
|
|---|
| 16 | * 23.3.2, class template array:
|
|---|
| 17 | */
|
|---|
| 18 |
|
|---|
| 19 | template<class T, size_t N>
|
|---|
| 20 | struct array
|
|---|
| 21 | {
|
|---|
| 22 | using value_type = T;
|
|---|
| 23 | using reference = T&;
|
|---|
| 24 | using const_reference = const T&;
|
|---|
| 25 | using size_type = size_t;
|
|---|
| 26 | using difference_type = ptrdiff_t;
|
|---|
| 27 | using pointer = T*;
|
|---|
| 28 | using const_pointer = const T*;
|
|---|
| 29 | using iterator = pointer;
|
|---|
| 30 | using const_iterator = const_pointer;
|
|---|
| 31 | using reverse_iterator = std::reverse_iterator<iterator>;
|
|---|
| 32 | using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * Note: In the case of N == 0 the standard mandates that
|
|---|
| 36 | * begin() == end() which is achieved by setting the size
|
|---|
| 37 | * to 1. Return value of data() is unspecified and front()
|
|---|
| 38 | * and back() cause undefined behavior.
|
|---|
| 39 | */
|
|---|
| 40 | value_type elems[N ? N : 1];
|
|---|
| 41 |
|
|---|
| 42 | void fill(const T& x)
|
|---|
| 43 | {
|
|---|
| 44 | fill_n(begin(), N, x);
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | void swap(array& other)
|
|---|
| 48 | // TODO: Does not find declval atm :/
|
|---|
| 49 | /* noexcept(noexcept(swap(declval<T&>(), declval<T&>()))) */
|
|---|
| 50 | {
|
|---|
| 51 | swap_ranges(begin(), end(), other.begin());
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | iterator begin() noexcept
|
|---|
| 55 | {
|
|---|
| 56 | return &elems[0];
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | iterator end() noexcept
|
|---|
| 60 | {
|
|---|
| 61 | return &elems[0] + N;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | reverse_iterator rbegin() noexcept
|
|---|
| 65 | {
|
|---|
| 66 | return make_reverse_iterator(end());
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | reverse_iterator rend() noexcept
|
|---|
| 70 | {
|
|---|
| 71 | return make_reverse_iterator(begin());
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | const_iterator cbegin() const noexcept
|
|---|
| 75 | {
|
|---|
| 76 | return &elems[0];
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | const_iterator cend() const noexcept
|
|---|
| 80 | {
|
|---|
| 81 | return &elems[0] + N;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | const_reverse_iterator crbegin() const noexcept
|
|---|
| 85 | {
|
|---|
| 86 | return make_reverse_iterator(end());
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | const_reverse_iterator crend() const noexcept
|
|---|
| 90 | {
|
|---|
| 91 | return make_reverse_iterator(begin());
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | reference operator[](size_type idx)
|
|---|
| 95 | {
|
|---|
| 96 | return elems[idx];
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | constexpr const_reference operator[](size_type idx) const
|
|---|
| 100 | {
|
|---|
| 101 | return elems[idx];
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | reference at(size_type idx)
|
|---|
| 105 | {
|
|---|
| 106 | // TODO: Bounds checking.
|
|---|
| 107 | return elems[idx];
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | constexpr const_reference at(size_type idx) const
|
|---|
| 111 | {
|
|---|
| 112 | // TODO: Bounds checking.
|
|---|
| 113 | return elems[idx];
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | reference front()
|
|---|
| 117 | {
|
|---|
| 118 | return elems[0];
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | constexpr const_reference front() const
|
|---|
| 122 | {
|
|---|
| 123 | return elems[0];
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | reference back()
|
|---|
| 127 | {
|
|---|
| 128 | return elems[N - 1];
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | constexpr const_reference back() const
|
|---|
| 132 | {
|
|---|
| 133 | return elems[N - 1];
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | pointer data() noexcept
|
|---|
| 137 | {
|
|---|
| 138 | return &elems[0];
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | const_pointer data() const noexcept
|
|---|
| 142 | {
|
|---|
| 143 | return &elems[0];
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | size_type size() const noexcept
|
|---|
| 147 | {
|
|---|
| 148 | return N;
|
|---|
| 149 | }
|
|---|
| 150 | };
|
|---|
| 151 |
|
|---|
| 152 | template<class T, size_t N>
|
|---|
| 153 | void swap(array<T, N>& lhs, array<T, N>& rhs) noexcept(noexcept(lhs.swap(rhs)))
|
|---|
| 154 | {
|
|---|
| 155 | lhs.swap(rhs);
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | /**
|
|---|
| 159 | * 23.3.2.9, tuple interface for class template array:
|
|---|
| 160 | */
|
|---|
| 161 |
|
|---|
| 162 | template<class>
|
|---|
| 163 | struct tuple_size;
|
|---|
| 164 |
|
|---|
| 165 | template<class T, size_t N>
|
|---|
| 166 | struct tuple_size<array<T, N>>
|
|---|
| 167 | : integral_constant<size_t, N>
|
|---|
| 168 | { /* DUMMY BODY */ };
|
|---|
| 169 |
|
|---|
| 170 | template<size_t, class>
|
|---|
| 171 | struct tuple_element;
|
|---|
| 172 |
|
|---|
| 173 | template<size_t I, class T, size_t N>
|
|---|
| 174 | struct tuple_element<I, array<T, N>>
|
|---|
| 175 | : aux::type_is<T>
|
|---|
| 176 | { /* DUMMY BODY */ };
|
|---|
| 177 |
|
|---|
| 178 | template<size_t I, class T, size_t N>
|
|---|
| 179 | constexpr T& get(array<T, N>& arr) noexcept
|
|---|
| 180 | {
|
|---|
| 181 | static_assert(I < N, "index out of bounds");
|
|---|
| 182 |
|
|---|
| 183 | return arr[I];
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | template<size_t I, class T, size_t N>
|
|---|
| 187 | constexpr T&& get(array<T, N>&& arr) noexcept
|
|---|
| 188 | {
|
|---|
| 189 | return move(get<I>(arr));
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | template<size_t I, class T, size_t N>
|
|---|
| 193 | constexpr const T& get(const array<T, N>& arr) noexcept
|
|---|
| 194 | {
|
|---|
| 195 | static_assert(I < N, "index out of bounds");
|
|---|
| 196 |
|
|---|
| 197 | return arr[I];
|
|---|
| 198 | }
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | #endif
|
|---|