source: mainline/uspace/lib/cpp/include/impl/array.hpp@ 604038c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 604038c was 4a8d37c6, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: added missing array algorithms

  • Property mode set to 100644
File size: 4.9 KB
Line 
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>
33
34namespace std
35{
36 /**
37 * 23.3.2, class template array:
38 */
39
40 template<class T, size_t N>
41 struct array
42 {
43 using value_type = T;
44 using reference = T&;
45 using const_reference = const T&;
46 using size_type = size_t;
47 using difference_type = ptrdiff_t;
48 using pointer = T*;
49 using const_pointer = const T*;
50 using iterator = pointer;
51 using const_iterator = const_pointer;
52 using reverse_iterator = std::reverse_iterator<iterator>;
53 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
54
55 /**
56 * Note: In the case of N == 0 the standard mandates that
57 * begin() == end() which is achieved by setting the size
58 * to 1. Return value of data() is unspecified and front()
59 * and back() cause undefined behavior.
60 */
61 value_type elems[N ? N : 1];
62
63 void fill(const T& x)
64 {
65 fill_n(begin(), N, x);
66 }
67
68 void swap(array& other) noexcept(noexcept(swap(declval<T&>(), declval<T&>())))
69 {
70 swap_ranges(begin(), end(), other.begin());
71 }
72
73 iterator begin() noexcept
74 {
75 return &elems[0];
76 }
77
78 iterator end() noexcept
79 {
80 return &elems[0] + N;
81 }
82
83 reverse_iterator rbegin() noexcept
84 {
85 return make_reverse_iterator(end());
86 }
87
88 reverse_iterator rend() noexcept
89 {
90 return make_reverse_iterator(begin());
91 }
92
93 const_iterator cbegin() const noexcept
94 {
95 return &elems[0];
96 }
97
98 const_iterator cend() const noexcept
99 {
100 return &elems[0] + N;
101 }
102
103 const_reverse_iterator crbegin() const noexcept
104 {
105 return make_reverse_iterator(end());
106 }
107
108 const_reverse_iterator crend() const noexcept
109 {
110 return make_reverse_iterator(begin());
111 }
112
113 reference operator[](size_type idx)
114 {
115 return elems[idx];
116 }
117
118 constexpr const_reference operator[](size_type idx) const
119 {
120 return elems[idx];
121 }
122
123 reference at(size_type idx)
124 {
125 // TODO: Bounds checking.
126 return elems[idx];
127 }
128
129 constexpr const_reference at(size_type idx) const
130 {
131 // TODO: Bounds checking.
132 return elems[idx];
133 }
134
135 reference front()
136 {
137 return elems[0];
138 }
139
140 constexpr const_reference front() const
141 {
142 return elems[0];
143 }
144
145 reference back()
146 {
147 return elems[N - 1];
148 }
149
150 constexpr const_reference back() const
151 {
152 return elems[N - 1];
153 }
154
155 pointer data() noexcept
156 {
157 return &elems[0];
158 }
159
160 const_pointer data() const noexcept
161 {
162 return &elems[0];
163 }
164
165 size_type size() const noexcept
166 {
167 return N;
168 }
169 };
170
171 template<class T, size_t N>
172 void swap(array<T, N>& lhs, array<T, N>& rhs) noexcept(noexcept(lhs.swap(rhs)))
173 {
174 lhs.swap(rhs);
175 }
176
177 // TODO: tuple interface for array 23.3.2.9
178}
179
180#endif
Note: See TracBrowser for help on using the repository browser.