Stratax 0.2.0
Loading...
Searching...
No Matches
Print.hpp
1#pragma once
2
3#include <iterator>
4#include <ostream>
5#include <string>
6
7#include <stratax/core/containers/Matrix.hpp>
8#include <stratax/core/containers/Tensor.hpp>
9#include <stratax/core/containers/Vector.hpp>
10#include <stratax/core/algorithms/Conversions.hpp>
11#include <stratax/core/containers/Shape.hpp>
12
13namespace stratax::container {
14
15namespace detail {
16
27template<typename T>
28void print_tensor_recursive(
29 std::ostream& os,
30 const Tensor<T>& tensor,
31 std::size_t dim,
32 std::size_t offset,
33 std::size_t depth,
34 const char* sibling_separator)
35{
36 const auto& shape = tensor.shape();
37 const auto& strides = tensor.strides();
38
39 os << "[";
40
41 if (dim == shape.rank() - 1)
42 {
43 for (std::size_t i = 0; i < shape(dim); ++i)
44 {
45 os << tensor(offset + i * strides(dim));
46
47 if (i + 1 != shape(dim))
48 os << ", ";
49 }
50 }
51 else
52 {
53 os << '\n';
54
55 for (std::size_t i = 0; i < shape(dim); ++i)
56 {
57 os << std::string((depth + 1) * 4, ' ');
58 print_tensor_recursive(
59 os,
60 tensor,
61 dim + 1,
62 offset + i * strides(dim),
63 depth + 1,
64 sibling_separator);
65
66 if (i + 1 != shape(dim))
67 {
68 os << sibling_separator;
69 }
70 }
71
72 os << '\n';
73 os << std::string(depth * 4, ' ');
74 }
75
76 os << "]";
77}
78
79template<typename T>
80std::ostream& print_tensor_like(std::ostream& os, const Tensor<T>& tensor)
81{
82 if (tensor.shape().elements() == 0)
83 {
84 os << "[]";
85 return os;
86 }
87
88 print_tensor_recursive(os, tensor, 0, 0, 0, ",\n");
89 return os;
90}
91
92template<typename T>
93std::ostream& print_matrix_like(std::ostream& os, const Tensor<T>& tensor)
94{
95 print_tensor_recursive(os, tensor, 0, 0, 0, "\n");
96 return os;
97}
98
99}
100
109template<typename T>
110std::ostream& operator<<(std::ostream& os, const Vector<T>& vector)
111{
112 const auto tensor = to_tensor(vector);
113 return detail::print_tensor_like(os, tensor);
114}
115
124template<typename T>
125std::ostream& operator<<(std::ostream& os, const Matrix<T>& matrix)
126{
127 const auto tensor = to_tensor(matrix);
128 return detail::print_matrix_like(os, tensor);
129}
130
131template<typename T>
143std::ostream& operator<<(std::ostream& os, const Tensor<T>& tensor)
144{
145 return detail::print_tensor_like(os, tensor);
146}
147
148}
Stores a rank-2 Stratax array in row-major order.
Definition Matrix.hpp:29
Stores an N-dimensional Stratax array in contiguous memory.
Definition Tensor.hpp:31
Stores a rank-1 Stratax array in contiguous memory.
Definition Vector.hpp:27