#pragma once #include #include template void print_value(const T& value); template <> void print_value(const bool& value) { printf("%s", value ? "true" : "false"); } template <> void print_value(const int8_t& value) { printf("%d", value); } template <> void print_value(const int32_t& value) { printf("%d", value); } template <> void print_value(const uint8_t& value) { printf("%u", value); } template <> void print_value(const uint32_t& value) { printf("%u", value); } template <> void print_value(const float& value) { printf("%f", value); } template <> void print_value(const double& value) { printf("%f", value); } void print_repeated(const char *str, int count) { for (int i = 0; i < count; i++) { printf("%s", str); } } template void print_array(int len, const T* as) { printf("["); for (int i = 0; i < len; i++) { if (i != 0) printf(", "); print_value(as[i]); } printf("]"); } template void __print_ndarray_aux(bool first, bool last, SizeT* cursor, SizeT depth, NDArray* ndarray) { // A really lazy recursive implementation // Add left padding unless its the first entry (since there would be "[[[" before it) if (!first) { print_repeated(" ", depth); } const SizeT dim = ndarray->shape[depth]; if (depth + 1 == ndarray->ndims) { // Recursed down to last dimension, print the values in a nice list printf("["); SizeT* indices = (SizeT*) __builtin_alloca(sizeof(SizeT) * ndarray->ndims); for (SizeT i = 0; i < dim; i++) { ndarray::basic::util::set_indices_by_nth(ndarray->ndims, ndarray->shape, indices, *cursor); ElementT* pelement = (ElementT*) ndarray::basic::get_pelement_by_indices(ndarray, indices); ElementT element = *pelement; if (i != 0) printf(", "); // List delimiter print_value(element); printf("(@"); print_array(ndarray->ndims, indices); printf(")"); (*cursor)++; } printf("]"); } else { printf("["); for (SizeT i = 0; i < ndarray->shape[depth]; i++) { __print_ndarray_aux( i == 0, // first? i + 1 == dim, // last? cursor, depth + 1, ndarray ); } printf("]"); } // Add newline unless its the last entry (since there will be "]]]" after it) if (!last) { print_repeated("\n", depth); } } template void print_ndarray(NDArray* ndarray) { if (ndarray->ndims == 0) { printf(""); } else { SizeT cursor = 0; __print_ndarray_aux(true, true, &cursor, 0, ndarray); } printf("\n"); }