TRIQS/nda
2.0.0
Multi-dimensional array library for C++
Toggle main menu visibility
Loading...
Searching...
No Matches
io.hpp
Go to the documentation of this file.
1
// Copyright (c) 2019--present, The Simons Foundation
2
// This file is part of TRIQS/nda and is licensed under the Apache License, Version 2.0.
3
// SPDX-License-Identifier: Apache-2.0
4
// See LICENSE in the root of this distribution for details.
5
10
11
#pragma once
12
13
#include "
./clef.hpp
"
14
15
#include <functional>
16
#include <iostream>
17
#include <tuple>
18
#include <type_traits>
19
#include <utility>
20
21
namespace
nda::clef {
22
27
35
template
<
int
N>
36
std::ostream &
operator<<
(std::ostream &sout,
placeholder<N>
) {
37
return
sout <<
"_"
<< N;
38
}
39
48
template
<
typename
T>
49
std::ostream &
operator<<
(std::ostream &sout, std::reference_wrapper<T>
const
&wrapper) {
50
return
sout << wrapper.get();
51
}
52
53
// Overload of nda::clef::variadic_print for the case of an empty list of arguments.
54
inline
std::ostream &variadic_print(std::ostream &sout) {
return
sout; }
55
66
template
<
typename
T0,
typename
... Ts>
67
std::ostream &variadic_print(std::ostream &sout, T0 &&t0, Ts &&...ts) {
68
sout << std::forward<T0>(t0) << (
sizeof
...(Ts) > 0 ?
", "
:
""
);
69
variadic_print(sout, std::forward<Ts>(ts)...);
70
return
sout;
71
}
72
73
namespace
detail {
74
75
// Helper struct to print a `std::tuple` recursively to `std::ostream`.
76
template
<
int
C,
int
N>
77
struct
print_tuple_impl {
78
template
<
typename
Tuple>
79
void
operator()(std::ostream &sout, Tuple
const
&t) {
80
sout << std::get<C>(t) << (C != N - 1 ?
", "
:
""
);
81
print_tuple_impl<C + 1, N>()(sout, t);
82
}
83
};
84
85
// Helper struct to print a `std::tuple` recursively to `std::ostream` (end of the recursion).
86
template
<
int
N>
87
struct
print_tuple_impl<N, N> {
88
template
<
typename
Tuple>
89
void
operator()(std::ostream &, Tuple
const
&) {}
90
};
91
92
}
// namespace detail
93
102
template
<
typename
Tuple>
103
std::ostream &
print_tuple
(std::ostream &sout, Tuple
const
&t) {
104
detail::print_tuple_impl<1, std::tuple_size_v<Tuple>>()(sout, t);
105
return
sout;
106
}
107
117
template
<
typename
Tag,
typename
L>
118
requires
std::is_base_of_v<tags::unary_op, Tag>
119
std::ostream &
operator<<
(std::ostream &sout,
expr<Tag, L>
const
&ex) {
120
return
sout <<
"("
<< Tag::name() <<
" "
<< std::get<0>(ex.
childs
) <<
")"
;
121
}
122
133
template
<
typename
Tag,
typename
L,
typename
R>
134
requires
std::is_base_of_v<tags::binary_op, Tag>
135
std::ostream &
operator<<
(std::ostream &sout,
expr<Tag, L, R>
const
&ex) {
136
return
sout <<
"("
<< std::get<0>(ex.
childs
) <<
" "
<< Tag::name() <<
" "
<< std::get<1>(ex.
childs
) <<
")"
;
137
}
138
149
template
<
typename
C,
typename
A,
typename
B>
150
std::ostream &
operator<<
(std::ostream &sout,
expr<tags::if_else, C, A, B>
const
&ex) {
151
return
sout <<
"("
<< std::get<0>(ex.
childs
) <<
" ? "
<< std::get<1>(ex.
childs
) <<
" : "
<< std::get<2>(ex.
childs
) <<
")"
;
152
}
153
162
template
<
typename
... Ts>
163
std::ostream &
operator<<
(std::ostream &sout,
expr<tags::function, Ts...>
const
&ex) {
164
sout <<
"lambda"
165
<<
"("
;
166
print_tuple
(sout, ex.
childs
);
167
return
sout <<
")"
;
168
}
169
178
template
<
typename
... Ts>
179
std::ostream &
operator<<
(std::ostream &sout,
expr<tags::subscript, Ts...>
const
&ex) {
180
sout <<
"lambda"
181
<<
"["
;
182
print_tuple
(sout, ex.
childs
);
183
return
sout <<
"]"
;
184
}
185
194
template
<
typename
T>
195
std::ostream &
operator<<
(std::ostream &sout,
expr<tags::terminal, T>
const
&ex) {
196
return
sout << std::get<0>(ex.
childs
);
197
}
198
207
template
<
typename
T>
208
std::ostream &
operator<<
(std::ostream &sout,
expr<tags::subscript, T>
const
&ex) {
209
return
sout << std::get<0>(ex.
childs
) <<
"["
<< std::get<1>(ex.
childs
) <<
"]"
;
210
}
211
220
template
<
typename
T>
221
std::ostream &
operator<<
(std::ostream &sout,
expr<tags::negate, T>
const
&ex) {
222
return
sout <<
"-("
<< std::get<0>(ex.
childs
) <<
")"
;
223
}
224
234
template
<
typename
Expr,
int
... Is>
235
std::ostream &
operator<<
(std::ostream &sout,
make_fun_impl<Expr, Is...>
const
&f) {
236
sout <<
"lazy function : ("
;
237
variadic_print(sout,
placeholder<Is>
()...);
238
return
sout <<
") --> "
<< f.
obj
;
239
}
240
242
243
}
// namespace nda::clef
clef.hpp
Includes all relevant headers for the core clef library.
nda::clef::operator<<
__inline__ void operator<<(expr< tags::function, F, placeholder< Is >... > const &ex, RHS &&rhs)
Assign values to the underlying object of a lazy function call expression.
Definition
auto_assign.hpp:95
nda::clef::print_tuple
std::ostream & print_tuple(std::ostream &sout, Tuple const &t)
Print a std::tuple to std::ostream.
Definition
io.hpp:103
nda::clef::expr
Single node of the expression tree.
Definition
expression.hpp:65
nda::clef::expr::childs
childs_t childs
Child nodes of the current expression node.
Definition
expression.hpp:70
nda::clef::make_fun_impl
Helper struct to simplify calls to nda::clef::eval.
Definition
function.hpp:41
nda::clef::make_fun_impl::obj
T obj
Object to be evaluated.
Definition
function.hpp:43
nda::clef::placeholder
A placeholder is an empty struct, labelled by an int.
Definition
placeholder.hpp:51
nda
clef
io.hpp
Generated by
1.17.0