Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
tuple.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 <ext/ostream>
27
30
31namespace ext {
32
41template < typename ... Ts >
42class tuple : public std::tuple < Ts ... > {
43public:
47 using std::tuple< Ts ... >::tuple; // NOLINT(modernize-use-equals-default)
48
52 using std::tuple< Ts ... >::operator =;
53#ifndef __clang__
54
58 tuple ( ) = default;
59
63 tuple ( const tuple & other ) = default;
64
68 tuple ( tuple && other ) = default;
69
73 tuple & operator = ( tuple && other ) = default;
74
78 tuple & operator = ( const tuple & other ) = default;
79#endif
80
81#ifdef _LIBCPP_VERSION // FIXME remove when libc++ gets standard conforming implementation of tuple's operator = (see: https://reviews.llvm.org/D50106)
82 template < typename T, typename R >
83 void assignSingle ( T & t, R & r ) {
84 t = r;
85 }
86
87 template < typename ... Rs, size_t ... Indexes >
88 void assign ( const tuple < Rs ... > & other, std::index_sequence < Indexes ... > ) {
89 static_assert ( sizeof ... ( Ts ) == sizeof ... ( Rs ) );
90 ( assignSingle ( std::get < Indexes > ( * this ), std::get < Indexes > ( other ) ), ... );
91 }
92
93 template < typename ... Rs >
94 tuple & operator = ( const tuple < Rs ... > & other ) {
95 assign ( other, std::make_index_sequence < sizeof ... ( Rs ) > ( ) );
96 return * this;
97 }
98
99 template < typename T1, typename R2 >
100 tuple & operator = ( const pair < T1, R2 > & other ) {
101 static_assert ( 2 == sizeof ... ( Ts ) );
102 std::get < 0 > ( * this ) = other.first;
103 std::get < 1 > ( * this ) = other.second;
104 return * this;
105 }
106#endif
107
116 template < std::size_t I >
117 auto & get ( ) & {
118 return std::get < I > ( * this );
119 }
120
129 template < std::size_t I >
130 auto && get ( ) && {
131 return std::get < I > ( std::move ( * this ) );
132 }
133
142 template < std::size_t I >
143 const auto & get ( ) const & {
144 return std::get < I > ( * this );
145 }
146
153 template < class T, class R >
154 requires ( sizeof ... ( Ts ) == 2 )
155 operator ext::pair < T, R > ( ) const {
156 return ext::pair < T, R > ( std::get < 0 > ( * this ), std::get < 1 > ( * this ) );
157 }
158
159 template < class T2, class R2 >
160 constexpr auto operator <=> ( const ext::pair < T2, R2 > & second ) const;
161
162};
163
175template< class... Ts>
177 out << "(";
178
179 if constexpr ( sizeof ... ( Ts ) > 0 ) {
180 auto outCallback = [ & ] ( const auto & first, const auto & ... other ) {
181 out << first;
182 ( ( out << ", " << other ), ... );
183 };
184
185 std::apply ( outCallback, tuple );
186 }
187
188 out << ")";
189 return out;
190}
191
202template < typename ... Elements >
203constexpr auto make_tuple ( Elements && ... args ) {
204 return tuple < typename ext::strip_reference_wrapper < std::decay_t < Elements > >::type ... > ( std::forward < Elements > ( args ) ... );
205}
206
217template<typename ... Elements >
218constexpr tuple < Elements & ... > tie ( Elements & ... args ) noexcept {
219 return tuple < Elements & ... > ( args ... );
220}
221
222template < typename ... Ts >
223template < class T2, class R2 >
224constexpr auto tuple < Ts ... >::operator <=> ( const ext::pair < T2, R2 > & second ) const {
225 return (* this) <=> ext::tie ( second.first, second.second );
226}
227
228} /* namespace ext */
229
230namespace std {
231
238template < class ... Types >
239struct tuple_size < ext::tuple < Types ... > > : public std::integral_constant < std::size_t, sizeof...( Types ) > { };
240
250template < std::size_t I, class... Types >
251struct tuple_element < I, ext::tuple < Types... > > {
252 typedef typename std::tuple_element < I, std::tuple < Types ... > >::type type;
253};
254
255} /* namespace std */
Definition: ostream.h:14
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
Class extending the tuple class from the standard library. Original reason is to allow printing of th...
Definition: tuple.hpp:42
auto && get() &&
New behavior of get for rvalue referenced instance.
Definition: tuple.hpp:130
auto & get() &
Inherited behavior of get for non-const instance.
Definition: tuple.hpp:117
tuple()=default
tuple & operator=(tuple &&other)=default
tuple(const tuple &other)=default
tuple(tuple &&other)=default
const auto & get() const &
Inherited behavior of get fro const instance.
Definition: tuple.hpp:143
constexpr auto operator<=>(const ext::pair< T2, R2 > &second) const
Definition: tuple.hpp:224
constexpr decltype(auto) apply(F &&f, Tuple &&t)
Definition: AbstractionHelpers.hpp:27
p second
Definition: ToRegExpAlgebraic.h:126
Definition: sigHandler.cpp:20
constexpr tuple< Elements &... > tie(Elements &... args) noexcept
Helper of extended tuple of references construction. The tuple is constructed to reffer to values in ...
Definition: tuple.hpp:218
constexpr auto make_tuple(Elements &&... args)
Helper of extended tuple construction. The tuple is constructed from values pack, types are deduced.
Definition: tuple.hpp:203
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
Definition: FordFulkerson.hpp:16
std::tuple_element< I, std::tuple< Types... > >::type type
Definition: tuple.hpp:252