Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
functional.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 <functional>
27
28#include "type_traits.hpp"
29#include "typeindex.h"
30
31namespace ext {
32
33template < class T >
34class PolyComp {
35 const T & m_inst;
36
37public:
38 explicit PolyComp ( const T & inst ) : m_inst ( inst ) {
39 }
40
41 template < class R >
42 friend bool operator < ( const PolyComp < T > & first, const R & second ) {
43 if constexpr ( supports < std::less < > ( T, R ) >::value )
44 return first.m_inst < second;
45 else
46 return std::type_index ( typeid ( T ) ) < std::type_index ( typeid ( R ) );
47 }
48
49 template < class R >
50 friend bool operator < ( const R & first, const PolyComp < T > & second ) {
51 if constexpr ( supports < std::less < > ( R, T ) >::value )
52 return first < second.m_inst;
53 else
54 return std::type_index ( typeid ( R ) ) < std::type_index ( typeid ( T ) );
55 }
56
57};
58
59template < class T >
60PolyComp < T > poly_comp ( const T & inst ) {
61 return PolyComp < T > ( inst );
62}
63
64template < class ... Ts >
65class SliceComp {
66 std::tuple < const Ts & ... > m_data;
67
68 template < class T, size_t ... I >
69 static bool compare ( const SliceComp < Ts ... > & first, const T & second, std::index_sequence < I ... > ) {
70 return first.m_data < std::tie ( std::get < I > ( second ) ... );
71 }
72
73 template < class T, size_t ... I >
74 static bool compare ( const T & first, const SliceComp < Ts ... > & second, std::index_sequence < I ... > ) {
75 return std::tie ( std::get < I > ( first ) ... ) < second.m_data;
76 }
77
78public:
79 explicit SliceComp ( const Ts & ... data ) : m_data ( data ... ) {
80 }
81
82 template < class T >
83 friend bool operator < ( const SliceComp < Ts ... > & first, const T & second ) {
84 return compare < T > ( first, second, std::make_index_sequence < sizeof ... ( Ts ) > { } );
85 }
86
87 template < class T >
88 friend bool operator < ( const T & first, const SliceComp < Ts ... > & second ) {
89 return compare < T > ( first, second, std::make_index_sequence < sizeof ... ( Ts ) > { } );
90 }
91
92};
93
94template < class ... Ts >
95SliceComp < Ts ... > slice_comp ( const Ts & ... inst ) {
96 return SliceComp < Ts ... > ( inst ... );
97}
98
107template < class T >
108class reference_wrapper : public std::reference_wrapper < T > {
109public:
113 using std::reference_wrapper < T >::reference_wrapper;
114
118 using std::reference_wrapper < T >::operator =;
119
120#ifndef __clang__
121
125 reference_wrapper ( const reference_wrapper & other ) = default;
126
130 reference_wrapper & operator = ( const reference_wrapper & other ) = default;
131
135 reference_wrapper & operator = ( const T & object ) {
136 return * this = reference_wrapper < T > ( object );
137 }
138#endif
139
143 using std::reference_wrapper < T >::operator T &;
144
153 auto operator <=> ( const reference_wrapper < T > & other ) const {
154 return this->get ( ) <=> other.get ( );
155 }
156
165 bool operator == ( const reference_wrapper < T > & other ) const {
166 return this->get ( ) == other.get ( );
167 }
168};
169
170} /* namespace ext */
171
Definition: functional.hpp:34
PolyComp(const T &inst)
Definition: functional.hpp:38
friend bool operator<(const PolyComp< T > &first, const R &second)
Definition: functional.hpp:42
Definition: functional.hpp:65
SliceComp(const Ts &... data)
Definition: functional.hpp:79
friend bool operator<(const SliceComp< Ts ... > &first, const T &second)
Definition: functional.hpp:83
Class extending the reference wrapper class from the standard library. Original reason is to allow it...
Definition: functional.hpp:108
reference_wrapper & operator=(const reference_wrapper &other)=default
auto operator<=>(const reference_wrapper< T > &other) const
Specialisation of less than operator for reference_wrapper vector.
Definition: functional.hpp:153
bool operator==(const reference_wrapper< T > &other) const
Specialisation of equals operator for reference_wrapper vector.
Definition: functional.hpp:165
reference_wrapper(const reference_wrapper &other)=default
p second
Definition: ToRegExpAlgebraic.h:126
Definition: AutomatonCompare.h:29
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
SliceComp< Ts ... > slice_comp(const Ts &... inst)
Definition: functional.hpp:95
PolyComp< T > poly_comp(const T &inst)
Definition: functional.hpp:60
auto & get(ext::ptr_array< Type, N > &tpl)
Specialisation of get function for pointer arrays.
Definition: ptr_array.hpp:693
Definition: type_traits.hpp:77