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--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 "./arithmetic.hpp"
14#include "./concepts.hpp"
15#include "./array_adapter.hpp"
16#include "./layout/idx_map.hpp"
18#include "./map.hpp"
19#include "./traits.hpp"
20
21#include <cstdint>
22#include <ostream>
23
24namespace nda {
25
30
38 inline std::ostream &operator<<(std::ostream &sout, layout_prop_e p) {
39 return sout << (has_contiguous(p) ? "contiguous " : " ") << (has_strided_1d(p) ? "strided_1d " : " ")
40 << (has_smallest_stride_is_one(p) ? "smallest_stride_is_one " : " ");
41 }
42
54 template <int Rank, uint64_t StaticExtents, uint64_t StrideOrder, layout_prop_e LayoutProp>
55 std::ostream &operator<<(std::ostream &sout, idx_map<Rank, StaticExtents, StrideOrder, LayoutProp> const &idxm) {
56 return sout << " Lengths : " << idxm.lengths() << "\n"
57 << " Strides : " << idxm.strides() << "\n"
58 << " StaticExtents : " << decode<Rank>(StaticExtents) << "\n"
59 << " MemoryStrideOrder : " << idxm.stride_order << "\n"
60 << " Flags : " << LayoutProp << "\n";
61 }
62
64
69
78 template <typename A>
79 std::ostream &operator<<(std::ostream &sout, A const &a)
81 {
82 // 1-dimensional array/view
83 if constexpr (A::rank == 1) {
84 sout << "[";
85 auto const &len = a.indexmap().lengths();
86 for (size_t i = 0; i < len[0]; ++i) sout << (i > 0 ? "," : "") << a(i);
87 sout << "]";
88 }
89
90 // 2-dimensional array/view
91 if constexpr (A::rank == 2) {
92 auto const &len = a.indexmap().lengths();
93 sout << "\n[";
94 for (size_t i = 0; i < len[0]; ++i) {
95 sout << (i == 0 ? "[" : " [");
96 for (size_t j = 0; j < len[1]; ++j) sout << (j > 0 ? "," : "") << a(i, j);
97 sout << "]" << (i == len[0] - 1 ? "" : "\n");
98 }
99 sout << "]";
100 }
101
102 // FIXME : not very pretty, do better here, but that was the arrays way
103 // higher-dimensional array/view (flat representation)
104 if constexpr (A::rank > 2) {
105 sout << "[";
106 for (bool first = true; auto &v : a) {
107 sout << (first ? "" : ",") << v;
108 first = false;
109 }
110 sout << "]";
111 }
112
113 return sout;
114 }
115
125 template <int R, typename F>
126 std::ostream &operator<<(std::ostream &sout, array_adapter<R, F> const &aa) {
127 return sout << "array_adapter of shape " << aa.shape();
128 }
129
131 // Forward declarations (necessary for libclang parsing).
132 template <char OP, Array A>
133 struct expr_unary;
134
135 template <char OP, ArrayOrScalar L, ArrayOrScalar R>
136 struct expr;
138
148 template <char OP, Array A>
149 std::ostream &operator<<(std::ostream &sout, expr_unary<OP, A> const &ex) {
150 return sout << OP << ex.a;
151 }
152
163 template <char OP, ArrayOrScalar L, ArrayOrScalar R>
164 std::ostream &operator<<(std::ostream &sout, expr<OP, L, R> const &ex) {
165 return sout << "(" << ex.l << " " << OP << " " << ex.r << ")";
166 }
167
176 template <typename F, typename... As>
177 std::ostream &operator<<(std::ostream &sout, expr_call<F, As...> const &) {
178 return sout << "mapped"; //array<value_type, std::decay_t<A>::rank>(x);
179 }
180
182
183} // 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:90
static constexpr std::array< int, Rank > stride_order
Decoded stride order.
Definition idx_map.hpp:108
std::array< long, Rank > const & lengths() const noexcept
Get the extents of all dimensions.
Definition idx_map.hpp:165
std::array< long, Rank > const & strides() const noexcept
Get the strides of all dimensions.
Definition idx_map.hpp:171
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:152
constexpr bool has_contiguous(layout_prop_e lp)
Checks if a layout property has the contiguous property.
Definition traits.hpp:271
constexpr bool has_strided_1d(layout_prop_e lp)
Checks if a layout property has the strided_1d property.
Definition traits.hpp:255
std::ostream & operator<<(std::ostream &os, range::all_t) noexcept
Write nda::range::all_t to a std::ostream as _.
Definition range.hpp:44
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:263
layout_prop_e
Compile-time guarantees of the memory layout of an array/view.
Definition traits.hpp:211
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:79
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.