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
22#pragma once
23
24#include "./arithmetic.hpp"
25#include "./concepts.hpp"
26#include "./array_adapter.hpp"
27#include "./layout/idx_map.hpp"
29#include "./map.hpp"
30#include "./traits.hpp"
31
32#include <cstdint>
33#include <ostream>
34
35namespace nda {
36
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
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
89 template <typename A>
90 std::ostream &operator<<(std::ostream &sout, A const &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
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
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;
149
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
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
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
194} // namespace nda
Provides lazy expressions for nda::Array types.
Provides an array adapter class.
Adapter that consists of a shape and a callable object, which takes R integers as arguments (just lik...
auto const & shape() const
Get shape of the adapter.
Layout that specifies how to map multi-dimensional indices to a linear/flat index.
Definition idx_map.hpp:103
static constexpr std::array< int, Rank > stride_order
Decoded stride order.
Definition idx_map.hpp:121
std::array< long, Rank > const & lengths() const noexcept
Get the extents of all dimensions.
Definition idx_map.hpp:178
std::array< long, Rank > const & strides() const noexcept
Get the strides of all dimensions.
Definition idx_map.hpp:184
Provides concepts for the nda library.
constexpr bool is_regular_or_view_v
Constexpr variable that is true if type A is either a regular array or a view.
Definition traits.hpp:163
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
std::ostream & operator<<(std::ostream &os, range::all_t) noexcept
Write nda::range::all_t to a std::ostream as _.
Definition range.hpp:57
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
constexpr std::array< int, N > decode(uint64_t binary_representation)
Decode a uint64_t into a std::array<int, N>.
Provides a class that maps multi-dimensional indices to a linear index and vice versa.
Provides lazy function calls on arrays/views.
Provides utilities to work with permutations and to compactly encode/decode std::array objects.
A lazy function call expression on arrays/views.
Definition map.hpp:90
Lazy unary expression for nda::Array types.
A a
nda::Array object.
Lazy binary expression for nda::ArrayOrScalar types.
L l
nda::ArrayOrScalar left hand side operand.
R r
nda::ArrayOrScalar right hand side operand.
Provides type traits for the nda library.