TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
print.hpp
Go to the documentation of this file.
1// Copyright (c) 2019-2023 Simons Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0.txt
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Authors: Olivier Parcollet, Nils Wentzell
16
17/**
18 * @file
19 * @brief Provides various overloads of the operator<< for nda related objects.
20 */
21
22#pragma once
23
24#include "./arithmetic.hpp"
25#include "./concepts.hpp"
26#include "./array_adapter.hpp"
27#include "./layout/idx_map.hpp"
28#include "./layout/permutation.hpp"
29#include "./map.hpp"
30#include "./traits.hpp"
31
32#include <cstdint>
33#include <ostream>
34
35namespace nda {
36
37 /**
38 * @addtogroup layout_utils
39 * @{
40 */
41
42 /**
43 * @brief Write an nda::layout_prop_e to a std::ostream.
44 *
45 * @param sout std::ostream object.
46 * @param p nda::layout_prop_e object.
47 * @return Reference to std::ostream object.
48 */
49 inline std::ostream &operator<<(std::ostream &sout, layout_prop_e p) {
50 return sout << (has_contiguous(p) ? "contiguous " : " ") << (has_strided_1d(p) ? "strided_1d " : " ")
51 << (has_smallest_stride_is_one(p) ? "smallest_stride_is_one " : " ");
52 }
53
54 /**
55 * @brief Write an nda::idx_map to a std::ostream.
56 *
57 * @tparam Rank Rank of the nda::idx_map.
58 * @tparam StaticExtents StaticExtents of the nda::idx_map.
59 * @tparam StrideOrder StrideOrder of the nda::idx_map.
60 * @tparam LayoutProp Layout property of the nda::idx_map.
61 * @param sout std::ostream object.
62 * @param idxm nda::idx_map object.
63 * @return Reference to std::ostream object.
64 */
65 template <int Rank, uint64_t StaticExtents, uint64_t StrideOrder, layout_prop_e LayoutProp>
66 std::ostream &operator<<(std::ostream &sout, idx_map<Rank, StaticExtents, StrideOrder, LayoutProp> const &idxm) {
67 return sout << " Lengths : " << idxm.lengths() << "\n"
68 << " Strides : " << idxm.strides() << "\n"
69 << " StaticExtents : " << decode<Rank>(StaticExtents) << "\n"
70 << " MemoryStrideOrder : " << idxm.stride_order << "\n"
71 << " Flags : " << LayoutProp << "\n";
72 }
73
74 /** @} */
75
76 /**
77 * @addtogroup av_utils
78 * @{
79 */
80
81 /**
82 * @brief Write an nda::basic_array or nda::basic_array_view to a std::ostream.
83 *
84 * @tparam A Type of the nda::basic_array or nda::basic_array_view.
85 * @param sout std::ostream object.
86 * @param a nda::basic_array or nda::basic_array_view object.
87 * @return Reference to std::ostream object.
88 */
89 template <typename A>
90 std::ostream &operator<<(std::ostream &sout, A const &a)
91 requires(is_regular_or_view_v<A>)
92 {
93 // 1-dimensional array/view
94 if constexpr (A::rank == 1) {
95 sout << "[";
96 auto const &len = a.indexmap().lengths();
97 for (size_t i = 0; i < len[0]; ++i) sout << (i > 0 ? "," : "") << a(i);
98 sout << "]";
99 }
100
101 // 2-dimensional array/view
102 if constexpr (A::rank == 2) {
103 auto const &len = a.indexmap().lengths();
104 sout << "\n[";
105 for (size_t i = 0; i < len[0]; ++i) {
106 sout << (i == 0 ? "[" : " [");
107 for (size_t j = 0; j < len[1]; ++j) sout << (j > 0 ? "," : "") << a(i, j);
108 sout << "]" << (i == len[0] - 1 ? "" : "\n");
109 }
110 sout << "]";
111 }
112
113 // FIXME : not very pretty, do better here, but that was the arrays way
114 // higher-dimensional array/view (flat representation)
115 if constexpr (A::rank > 2) {
116 sout << "[";
117 for (bool first = true; auto &v : a) {
118 sout << (first ? "" : ",") << v;
119 first = false;
120 }
121 sout << "]";
122 }
123
124 return sout;
125 }
126
127 /**
128 * @brief Write an nda::array_adapter to a std::ostream.
129 *
130 * @tparam R Rank of the nda::array_adapter.
131 * @tparam F Callable type of the nda::array_adapter.
132 * @param sout std::ostream object.
133 * @param aa nda::array_adapter object.
134 * @return Reference to std::ostream object.
135 */
136 template <int R, typename F>
137 std::ostream &operator<<(std::ostream &sout, array_adapter<R, F> const &aa) {
138 return sout << "array_adapter of shape " << aa.shape();
139 }
140
141 /// @cond
142 // Forward declarations (necessary for libclang parsing).
143 template <char OP, Array A>
144 struct expr_unary;
145
146 template <char OP, ArrayOrScalar L, ArrayOrScalar R>
147 struct expr;
148 /// @endcond
149
150 /**
151 * @brief Write an nda::expr_unary to a std::ostream.
152 *
153 * @tparam OP Unary operator.
154 * @tparam A nda::Array type.
155 * @param sout std::ostream object.
156 * @param ex nda::expr_unary object.
157 * @return Reference to std::ostream object.
158 */
159 template <char OP, Array A>
160 std::ostream &operator<<(std::ostream &sout, expr_unary<OP, A> const &ex) {
161 return sout << OP << ex.a;
162 }
163
164 /**
165 * @brief Write an nda::expr to a std::ostream.
166 *
167 * @tparam OP Binary operator.
168 * @tparam L nda::ArrayOrScalar type of left hand side.
169 * @tparam R nda::ArrayOrScalar type of right hand side.
170 * @param sout std::ostream object.
171 * @param ex nda::expr object.
172 * @return Reference to std::ostream object.
173 */
174 template <char OP, ArrayOrScalar L, ArrayOrScalar R>
175 std::ostream &operator<<(std::ostream &sout, expr<OP, L, R> const &ex) {
176 return sout << "(" << ex.l << " " << OP << " " << ex.r << ")";
177 }
178
179 /**
180 * @brief Write an nda::expr_call to a std::ostream.
181 *
182 * @tparam F Callable type.
183 * @tparam As Argument types.
184 * @param sout std::ostream object.
185 * @return Reference to std::ostream object.
186 */
187 template <typename F, typename... As>
188 std::ostream &operator<<(std::ostream &sout, expr_call<F, As...> const &) {
189 return sout << "mapped"; //array<value_type, std::decay_t<A>::rank>(x);
190 }
191
192 /** @} */
193
194} // namespace nda
Adapter that consists of a shape and a callable object, which takes R integers as arguments (just lik...
Layout that specifies how to map multi-dimensional indices to a linear/flat index.
Definition idx_map.hpp:103
constexpr bool has_contiguous(layout_prop_e lp)
Checks if a layout property has the contiguous property.
Definition traits.hpp:282
constexpr bool has_strided_1d(layout_prop_e lp)
Checks if a layout property has the strided_1d property.
Definition traits.hpp:266
constexpr bool has_smallest_stride_is_one(layout_prop_e lp)
Checks if a layout property has the smallest_stride_is_one property.
Definition traits.hpp:274
layout_prop_e
Compile-time guarantees of the memory layout of an array/view.
Definition traits.hpp:222
A lazy function call expression on arrays/views.
Definition map.hpp:90
Lazy unary expression for nda::Array types.
Lazy binary expression for nda::ArrayOrScalar types.