[aab972f] | 1 | /*
|
---|
| 2 | * Copyright (c) 2017 Jaroslav Jindrak
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #ifndef LIBCPP_ARRAY
|
---|
| 30 | #define LIBCPP_ARRAY
|
---|
| 31 |
|
---|
| 32 | #include <iterator>
|
---|
[b4b01cb] | 33 | #include <utility>
|
---|
[aab972f] | 34 |
|
---|
| 35 | namespace std
|
---|
| 36 | {
|
---|
| 37 | /**
|
---|
| 38 | * 23.3.2, class template array:
|
---|
| 39 | */
|
---|
| 40 |
|
---|
| 41 | template<class T, size_t N>
|
---|
| 42 | struct array
|
---|
| 43 | {
|
---|
| 44 | using value_type = T;
|
---|
| 45 | using reference = T&;
|
---|
| 46 | using const_reference = const T&;
|
---|
| 47 | using size_type = size_t;
|
---|
| 48 | using difference_type = ptrdiff_t;
|
---|
| 49 | using pointer = T*;
|
---|
| 50 | using const_pointer = const T*;
|
---|
| 51 | using iterator = pointer;
|
---|
| 52 | using const_iterator = const_pointer;
|
---|
| 53 | using reverse_iterator = std::reverse_iterator<iterator>;
|
---|
| 54 | using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
---|
| 55 |
|
---|
| 56 | /**
|
---|
| 57 | * Note: In the case of N == 0 the standard mandates that
|
---|
| 58 | * begin() == end() which is achieved by setting the size
|
---|
| 59 | * to 1. Return value of data() is unspecified and front()
|
---|
| 60 | * and back() cause undefined behavior.
|
---|
| 61 | */
|
---|
| 62 | value_type elems[N ? N : 1];
|
---|
| 63 |
|
---|
| 64 | void fill(const T& x)
|
---|
| 65 | {
|
---|
[4a8d37c6] | 66 | fill_n(begin(), N, x);
|
---|
[aab972f] | 67 | }
|
---|
| 68 |
|
---|
[b4b01cb] | 69 | void swap(array& other)
|
---|
| 70 | // TODO: Does not find declval atm :/
|
---|
| 71 | /* noexcept(noexcept(swap(declval<T&>(), declval<T&>()))) */
|
---|
[aab972f] | 72 | {
|
---|
[4a8d37c6] | 73 | swap_ranges(begin(), end(), other.begin());
|
---|
[aab972f] | 74 | }
|
---|
| 75 |
|
---|
| 76 | iterator begin() noexcept
|
---|
| 77 | {
|
---|
| 78 | return &elems[0];
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | iterator end() noexcept
|
---|
| 82 | {
|
---|
| 83 | return &elems[0] + N;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | reverse_iterator rbegin() noexcept
|
---|
| 87 | {
|
---|
| 88 | return make_reverse_iterator(end());
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | reverse_iterator rend() noexcept
|
---|
| 92 | {
|
---|
| 93 | return make_reverse_iterator(begin());
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | const_iterator cbegin() const noexcept
|
---|
| 97 | {
|
---|
| 98 | return &elems[0];
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | const_iterator cend() const noexcept
|
---|
| 102 | {
|
---|
| 103 | return &elems[0] + N;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | const_reverse_iterator crbegin() const noexcept
|
---|
| 107 | {
|
---|
| 108 | return make_reverse_iterator(end());
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | const_reverse_iterator crend() const noexcept
|
---|
| 112 | {
|
---|
| 113 | return make_reverse_iterator(begin());
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | reference operator[](size_type idx)
|
---|
| 117 | {
|
---|
| 118 | return elems[idx];
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | constexpr const_reference operator[](size_type idx) const
|
---|
| 122 | {
|
---|
| 123 | return elems[idx];
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[529ebfb8] | 126 | reference at(size_type idx)
|
---|
| 127 | {
|
---|
| 128 | // TODO: Bounds checking.
|
---|
| 129 | return elems[idx];
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | constexpr const_reference at(size_type idx) const
|
---|
| 133 | {
|
---|
| 134 | // TODO: Bounds checking.
|
---|
| 135 | return elems[idx];
|
---|
| 136 | }
|
---|
[aab972f] | 137 |
|
---|
| 138 | reference front()
|
---|
| 139 | {
|
---|
| 140 | return elems[0];
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | constexpr const_reference front() const
|
---|
| 144 | {
|
---|
| 145 | return elems[0];
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | reference back()
|
---|
| 149 | {
|
---|
| 150 | return elems[N - 1];
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | constexpr const_reference back() const
|
---|
| 154 | {
|
---|
| 155 | return elems[N - 1];
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | pointer data() noexcept
|
---|
| 159 | {
|
---|
| 160 | return &elems[0];
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | const_pointer data() const noexcept
|
---|
| 164 | {
|
---|
| 165 | return &elems[0];
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | size_type size() const noexcept
|
---|
| 169 | {
|
---|
| 170 | return N;
|
---|
| 171 | }
|
---|
| 172 | };
|
---|
| 173 |
|
---|
| 174 | template<class T, size_t N>
|
---|
| 175 | void swap(array<T, N>& lhs, array<T, N>& rhs) noexcept(noexcept(lhs.swap(rhs)))
|
---|
| 176 | {
|
---|
| 177 | lhs.swap(rhs);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | // TODO: tuple interface for array 23.3.2.9
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | #endif
|
---|