Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Concepts
array.hpp
Go to the documentation of this file.
1
6/*
7 * This file is part of Algorithms library toolkit.
8 * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz)
9
10 * Algorithms library toolkit is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14
15 * Algorithms library toolkit is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19
20 * You should have received a copy of the GNU General Public License
21 * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#pragma once
25
26#include <array>
27
28#include <ext/ostream>
29
30#include <extensions/range.hpp>
31
32namespace std {
33
34namespace experimental {
35
36namespace details {
37
38template <class D, class...> struct return_type_helper { using type = D; };
39template <class... Types>
40struct return_type_helper<void, Types...> : std::common_type<Types...> {};
41
42template <class D, class... Types>
43using return_type = std::array<typename return_type_helper<D, Types...>::type,
44 sizeof...(Types)>;
45}
46
47template < class D = void, class... Types>
48constexpr details::return_type<D, Types...> make_array(Types&&... t) {
49 return {{std::forward<Types>(t)... }};
50}
51
52} /* namespace experimental */
53
54} /* namespace std */ //FIXME remove whole thing when make_array gets from experimental to the standard
55
56namespace ext {
57
67template < class T, std::size_t N >
68class array : public std::array < T, N > {
69public:
73 using std::array < T, N >::array; // NOLINT(modernize-use-equals-default)
74
78 using std::array < T, N >::operator =;
79
80#ifndef __clang__
81
85 array ( ) = default;
86
90 array ( const array & other ) = default;
91
95 array ( array && other ) = default;
96
100 array & operator = ( array && other ) = default;
101
105 array & operator = ( const array & other ) = default;
106
107#endif
108
112 template < class ... Types >
113 requires ( std::is_same < T, typename std::remove_reference < Types >::type >::value && ... )
114 explicit array ( Types && ... args ) : std::array < T, N > ( std::experimental::make_array ( std::forward < Types > ( args ) ... ) ) {
115 }
116
123 auto begin ( ) & {
125 }
126
133 auto begin ( ) const & {
135 }
136
143 auto begin ( ) && {
144 return make_move_iterator ( this->begin ( ) );
145 }
146
153 auto end ( ) & {
154 return std::array < T, N >::end ( );
155 }
156
163 auto end ( ) const & {
164 return std::array < T, N >::end ( );
165 }
166
173 auto end ( ) && {
174 return make_move_iterator ( this->end ( ) );
175 }
176
183 auto range ( ) & {
184 auto endIter = end ( );
185 auto beginIter = begin ( );
186 return ext::iterator_range < decltype ( endIter ) > ( beginIter, endIter );
187 }
188
195 auto range ( ) const & {
196 auto endIter = end ( );
197 auto beginIter = begin ( );
198 return ext::iterator_range < decltype ( endIter ) > ( beginIter, endIter );
199 }
200
207 auto range ( ) && {
208 auto endIter = std::move ( * this ).end ( );
209 auto beginIter = std::move ( * this ).begin ( );
210 return ext::iterator_range < decltype ( endIter ) > ( beginIter, endIter );
211 }
212};
213
226template< class T, std::size_t N >
228 out << "[";
229
230 bool first = true;
231 for(const T& item : array) {
232 if(!first) out << ", ";
233 first = false;
234 out << item;
235 }
236
237 out << "]";
238 return out;
239}
240
253template < typename Base, typename ... Types >
254constexpr array < typename std::remove_reference < Base >::type, sizeof ... ( Types ) + 1 > make_array ( Base && first, Types && ... other ) {
255 return array < typename std::remove_reference < Base >::type, sizeof ... ( Types ) + 1 > ( std::forward < Base > ( first ), std::forward < Types > ( other ) ... );
256}
257
266template < typename Base >
269}
270
271} /* namespace ext */
272
Class extending the array class from the standard library. Original reason is to allow printing of th...
Definition: array.hpp:68
auto range() &&
Make range of move begin to end iterators.
Definition: array.hpp:207
auto end() &&
Inherited behavior of end for rvalues.
Definition: array.hpp:173
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: array.hpp:123
auto end() const &
Inherited behavior of end for const instance.
Definition: array.hpp:163
auto begin() const &
Inherited behavior of begin for const instance.
Definition: array.hpp:133
array()=default
array & operator=(array &&other)=default
array(array &&other)=default
auto end() &
Inherited behavior of end for non-const instance.
Definition: array.hpp:153
auto range() const &
Make range of non-const begin to end iterators.
Definition: array.hpp:195
auto begin() &&
Inherited behavior of begin for rvalues.
Definition: array.hpp:143
auto range() &
Make range of non-const begin to end iterators.
Definition: array.hpp:183
array(const array &other)=default
Implementation of iterator_range, i.e. pair of iterators. The class provides most notably begin and e...
Definition: range.hpp:24
Definition: ostream.h:14
Definition: sigHandler.cpp:20
constexpr array< typename std::remove_reference< Base >::type, sizeof ...(Types)+1 > make_array(Base &&first, Types &&... other)
Equivalent to the make_array from standard library but produces the ext::array.
Definition: array.hpp:254
std::ostream & operator<<(ext::reference_wrapper< std::ostream > &os, std::ostream &(*const func)(std::ostream &))
Overloaded function allowing same operations on wrapped output stream as on the actual output stream,...
Definition: GlobalData.cpp:33
auto begin(Container &&cont) -> decltype(std::forward(cont).begin())
Definition: iterator.hpp:900
void end()
Definition: measurements.cpp:19
std::array< typename return_type_helper< D, Types... >::type, sizeof...(Types)> return_type
Definition: array.hpp:44
constexpr details::return_type< D, Types... > make_array(Types &&... t)
Definition: array.hpp:48
Definition: FordFulkerson.hpp:16