microfmt
Zero-allocation C++ formatting for constrained environments
Loading...
Searching...
No Matches
math.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Jarosław Pelczar <jarek@jpelczar.com>
2//
3// SPDX-License-Identifier: BSD-2-Clause
4
5#pragma once
6
9#include "../microfmt.hpp"
10#include <cstddef>
11#include <string_view>
12#include <type_traits>
13
14namespace microfmt {
15
16// ============================================================================
17// Vector View (Vec2, Vec3, Vec4, N-dim)
18// ============================================================================
19
20template <typename T, size_t N> struct vec_view {
21 span<const T> data;
22};
23
24template <typename T> struct vec3_view {
25 T values[3];
26};
27
28template <typename T, size_t N> [[nodiscard]] constexpr auto vec(const T (&arr)[N]) noexcept {
29 return vec_view<T, N>{span<const T>(arr, N)};
30}
31
32template <typename T> [[nodiscard]] constexpr auto vec3(const T &x, const T &y, const T &z) noexcept {
33 return vec3_view<T>{{x, y, z}};
34}
35
36template <typename T, size_t N> struct formatter<vec_view<T, N>> {
37 microfmt::string_view forwarded_spec{""};
38
39 constexpr void parse(format_parse_context &ctx) noexcept { forwarded_spec = ctx.spec(); }
40
41 void format(const vec_view<T, N> &v, const sink &out) const noexcept {
42 out.put('<');
43 formatter<T> elem_fmt;
44 format_parse_context elem_ctx(forwarded_spec);
45 elem_fmt.parse(elem_ctx);
46
47 for (size_t i = 0; i < v.data.size(); ++i) {
48 if (i > 0)
49 out.write(", ");
50 elem_fmt.format(v.data[i], out);
51 }
52 out.put('>');
53 }
54};
55
56template <typename T> struct formatter<vec3_view<T>> {
57 microfmt::string_view forwarded_spec{""};
58
59 constexpr void parse(format_parse_context &ctx) noexcept { forwarded_spec = ctx.spec(); }
60
61 void format(const vec3_view<T> &v, const sink &out) const noexcept {
62 out.put('<');
63 formatter<T> elem_fmt;
64 format_parse_context elem_ctx(forwarded_spec);
65 elem_fmt.parse(elem_ctx);
66
67 for (size_t i = 0; i < 3; ++i) {
68 if (i > 0)
69 out.write(", ");
70 RELOCO_BEGIN_UNSAFE_BUFFER_USAGE;
71 elem_fmt.format(v.values[i], out);
72 RELOCO_END_UNSAFE_BUFFER_USAGE;
73 }
74 out.put('>');
75 }
76};
77
78// ============================================================================
79// Matrix View (Rows x Columns)
80// ============================================================================
81
82template <typename T, size_t Rows, size_t Cols> struct matrix_view {
83 span<const T> data; // Contiguous row-major storage (Rows * Cols)
84};
85
86template <typename T, size_t Rows, size_t Cols> [[nodiscard]] constexpr auto mat(const T (&arr)[Rows * Cols]) noexcept {
87 return matrix_view<T, Rows, Cols>{span<const T>(arr, Rows * Cols)};
88}
89
90template <typename T, size_t Rows, size_t Cols> struct formatter<matrix_view<T, Rows, Cols>> {
91 microfmt::string_view forwarded_spec{""};
92
93 constexpr void parse(format_parse_context &ctx) noexcept { forwarded_spec = ctx.spec(); }
94
95 void format(const matrix_view<T, Rows, Cols> &m, const sink &out) const noexcept {
96 out.write("[\n");
97 formatter<T> elem_fmt;
98 format_parse_context elem_ctx(forwarded_spec);
99 elem_fmt.parse(elem_ctx);
100
101 for (size_t r = 0; r < Rows; ++r) {
102 out.write(" [");
103 for (size_t c = 0; c < Cols; ++c) {
104 if (c > 0)
105 out.write(", ");
106 elem_fmt.format(m.data[r * Cols + c], out);
107 }
108 out.write("]\n");
109 }
110 out.put(']');
111 }
112};
113
114} // namespace microfmt
Definition microfmt.hpp:881
Definition assert.hpp:19
reloco::sink sink
Type-erased, zero-allocation output sink.
Definition microfmt.hpp:46
constexpr auto vec3(const T &x, const T &y, const T &z) noexcept
Definition math.hpp:32
constexpr auto vec(const T(&arr)[N]) noexcept
Definition math.hpp:28
constexpr auto mat(const T(&arr)[Rows *Cols]) noexcept
Definition math.hpp:86
void format(const matrix_view< T, Rows, Cols > &m, const sink &out) const noexcept
Definition math.hpp:95
constexpr void parse(format_parse_context &ctx) noexcept
Definition math.hpp:93
constexpr void parse(format_parse_context &ctx) noexcept
Definition math.hpp:59
void format(const vec3_view< T > &v, const sink &out) const noexcept
Definition math.hpp:61
void format(const vec_view< T, N > &v, const sink &out) const noexcept
Definition math.hpp:41
constexpr void parse(format_parse_context &ctx) noexcept
Definition math.hpp:39
Definition microfmt.hpp:985
Definition math.hpp:82
span< const T > data
Definition math.hpp:83
Definition math.hpp:24
T values[3]
Definition math.hpp:25
Definition math.hpp:20
span< const T > data
Definition math.hpp:21