Stratax 0.4.0rc1
Loading...
Searching...
No Matches
Print.hpp
Go to the documentation of this file.
1
4#pragma once
5
6#include <ostream>
7#include <string>
8
13
14namespace stratax::container {
15
16namespace detail {
17
19template<typename T>
20void print_value(std::ostream& os, const T& value)
21{
22 using type = std::remove_cvref_t<T>;
23
24 if constexpr (std::same_as<type, dtype::bool_>)
25 {
26 os << (value ? "true" : "false");
27 }
28 else if constexpr (
29 std::same_as<type, dtype::int8> ||
30 std::same_as<type, dtype::uint8>)
31 {
32 os << static_cast<int>(value);
33 }
34 else
35 {
36 os << value;
37 }
38}
39
41template<Array A>
43 std::ostream& os,
44 const A& array,
45 std::size_t dim,
46 std::size_t offset,
47 std::size_t depth,
48 const char* sibling_separator)
49{
50 const auto& shape = array.shape();
51 const auto logical_strides = shape.strides();
52
53 os << "[";
54
55 if (dim == shape.rank() - 1)
56 {
57 for (std::size_t i = 0; i < shape[dim]; ++i)
58 {
60 os,
61 array[offset + i * logical_strides[dim]]);
62
63 if (i + 1 != shape[dim])
64 os << ", ";
65 }
66 }
67 else
68 {
69 os << '\n';
70
71 for (std::size_t i = 0; i < shape[dim]; ++i)
72 {
73 os << std::string((depth + 1) * 4, ' ');
75 os,
76 array,
77 dim + 1,
78 offset + i * logical_strides[dim],
79 depth + 1,
80 sibling_separator);
81
82 if (i + 1 != shape[dim])
83 {
84 os << sibling_separator;
85 }
86 }
87
88 os << '\n';
89 os << std::string(depth * 4, ' ');
90 }
91
92 os << "]";
93}
94
102template<Array A>
103std::ostream& print_array(
104 std::ostream& os,
105 const A& array)
106{
107 if (array.empty())
108 {
109 os << "[]";
110 return os;
111 }
112
113 const char* sibling_separator =
114 array.rank() == 2 ? "\n" : ",\n";
115
117 os,
118 array,
119 0,
120 0,
121 0,
122 sibling_separator);
123 return os;
124}
125
126}
127
129template<typename T>
130std::ostream& operator<<(std::ostream& os, const Vector<T>& vector)
131{
132 return detail::print_array(os, vector);
133}
134
136template<typename T>
137std::ostream& operator<<(std::ostream& os, const Matrix<T>& matrix)
138{
139 return detail::print_array(os, matrix);
140}
141
143template<typename T>
144std::ostream& operator<<(std::ostream& os, const Tensor<T>& tensor)
145{
146 return detail::print_array(os, tensor);
147}
148
149}
150
151namespace stratax::core {
152
154template<typename T>
155std::ostream& operator<<(std::ostream& os, const ArrayView<T>& view)
156{
157 return container::detail::print_array(os, view);
158}
159
160}
Non-owning views and logical random-access iterators.
Rank-two owning arrays.
void print_value(std::ostream &os, const T &value)
Writes booleans as true/false and 8-bit integers as numbers; otherwise uses stream insertion.
Definition Print.hpp:20
std::ostream & print_array(std::ostream &os, const A &array)
Prints elements in logical row-major order with nested brackets. Empty arrays print as [],...
Definition Print.hpp:103
void print_recursive(std::ostream &os, const A &array, std::size_t dim, std::size_t offset, std::size_t depth, const char *sibling_separator)
Recursively prints a tensor using nested bracket notation.
Definition Print.hpp:42
std::ostream & operator<<(std::ostream &os, const Shape &shape)
Writes a shape using tuple notation.
Definition Shape.hpp:290
Arbitrary-rank owning arrays; rank zero is empty.
Rank-one owning arrays.
Two-dimensional owning array of supported dtype values.
Definition Matrix.hpp:49
Arbitrary-rank owning array of supported dtype values.
Definition Tensor.hpp:54
One-dimensional owning array of supported dtype values.
Definition Vector.hpp:47
Non-owning view of elements with unsigned element strides.
Definition ArrayView.hpp:38