Skip to content

Commit

Permalink
Merge pull request #5 from 4J-company/fix/output
Browse files Browse the repository at this point in the history
Merge fix/output into master
  • Loading branch information
dantibel authored Mar 15, 2024
2 parents 14416a6 + 788299f commit 52cd797
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 22 deletions.
2 changes: 2 additions & 0 deletions include/def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
#include <algorithm>
#include <concepts>
#include <iostream>
#include <sstream>
#include <numeric>
#include <numbers>
#include <ranges>
#include <format>
#include <mutex>
#include <cmath>
#include <bit>
Expand Down
23 changes: 23 additions & 0 deletions include/matr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,27 @@ namespace mr
};
} // namespace mr

// std::format support
namespace std {
template <mr::ArithmeticT T, size_t N>
struct formatter<mr::Matr<T, N>> {
template<typename ParseContext>
constexpr auto parse(ParseContext& ctx) {
// skip all format specifiers
return ctx.end();
}

template<typename FmtContext>
auto format(const mr::Matr<T, N> &m, FmtContext& ctx) const {
ostringstream out;
out << '(' << m[0] << ",\n";
for (size_t i = 1; i < N - 1; i++)
out << ' ' << m[i] << ",\n";
out << ' ' << m[N - 1] << ')';

return ranges::copy(move(out).str(), ctx.out()).out;
}
};
} // namespace std

#endif // __Matr_hpp_
42 changes: 29 additions & 13 deletions include/row.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,6 @@ namespace mr {
operator-(const DerivedT &rhs) noexcept {
return -rhs._data;
}

friend std::ostream &
operator<<(std::ostream &s, const DerivedT &v) noexcept {
s << '(';
for (size_t i = 0; i < DerivedT::size; i++) {
s << v[i] << (char)(',' * (i < DerivedT::size - 1)) << (char)(' ' * (i < DerivedT::size - 1));
}
s << ')' << DerivedT::strname;
return s;
}
};

template <typename DerivedT>
Expand Down Expand Up @@ -119,15 +109,22 @@ namespace mr {
lhs._data >>= rhs._data;
return lhs;
}

friend std::ostream &
operator<<(std::ostream &s, const DerivedT &v) noexcept {
s << '(';
for (size_t i = 0; i < DerivedT::size - 1; i++) {
s << v[i] << ", ";
}
s << v[DerivedT::size - 1] << ')';
return s;
}
};

template <ArithmeticT T, std::size_t N>
struct Row : RowOperators<Row<T, N>> {

public:
using SimdT = SimdImpl<T, N>;

static constexpr auto strname = "row";
static constexpr size_t size = N;

SimdT _data{};
Expand Down Expand Up @@ -203,4 +200,23 @@ namespace mr {
};
} // namespace mr

// std::format support
namespace std {
template <mr::ArithmeticT T, size_t N>
struct formatter<mr::Row<T, N>> {
template<typename ParseContext>
constexpr auto parse(ParseContext& ctx) {
// skip all format specifiers
return ctx.end();
}

template<typename FmtContext>
auto format(const mr::Row<T, N> &r, FmtContext& ctx) const {
ostringstream out;
out << r;
return ranges::copy(move(out).str(), ctx.out()).out;
}
};
} // namespace std

#endif // __Row_hpp_
43 changes: 34 additions & 9 deletions include/units.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ namespace mr
template <typename T>
concept UnitT = requires(T unit)
{
requires ArithmeticT<T>;
// member check
typename T::ValueT;
sizeof(T) == sizeof(typename T::ValueT);
requires ArithmeticT<typename T::ValueT>;

// constructor check
T{typename T::ValueT{}};

// operations check
{unit + unit} -> std::same_as<T>;
{unit - unit} -> std::same_as<T>;
{unit * ArithmeticT<T>} -> std::same_as<T>;
{unit / ArithmeticT<T>} -> std::same_as<T>;
{unit * typename T::ValueT{}} -> std::same_as<T>;
{unit / typename T::ValueT{}} -> std::same_as<T>;
};

// extract value from unit type (used for units compatibility)
Expand Down Expand Up @@ -48,9 +51,6 @@ namespace mr
public:
using ValueT = T;

static constexpr auto strname = "rad";
static constexpr std::size_t size = 1;

T _data;

constexpr Radians() noexcept {};
Expand All @@ -69,16 +69,18 @@ namespace mr

// comparison operator
[[nodiscard]] friend constexpr auto operator<=>(Radians lhs, Radians rhs) = default;

friend std::ostream & operator<<(std::ostream &os, const Radians &r) noexcept {
os << r._data << "rad";
return os;
}
};

template <std::floating_point T>
struct [[nodiscard]] Degrees : UnitOperators<Degrees<T>> {
public:
using ValueT = T;

static constexpr auto strname = "deg";
static constexpr std::size_t size = 1;

T _data;

explicit constexpr Degrees(T x) noexcept : _data(x) {};
Expand All @@ -97,6 +99,11 @@ namespace mr

// comparison operator
[[nodiscard]] friend constexpr auto operator<=>(Degrees lhs, Degrees rhs) = default;

friend std::ostream & operator<<(std::ostream &os, const Degrees &d) noexcept {
os << d._data << "deg";
return os;
}
};


Expand Down Expand Up @@ -142,4 +149,22 @@ namespace mr
}
}

// std::format support
namespace std {
template <mr::UnitT U>
struct formatter<U> {
template<typename ParseContext>
constexpr auto parse(ParseContext& ctx) {
// skip all format specifiers
return ctx.end();
}

template<typename FmtContext>
auto format(U u, FmtContext& ctx) const {
ostringstream out;
out << u;
return ranges::copy(move(out).str(), ctx.out()).out;
}
};
} // namespace std
#endif // __units_hpp_

0 comments on commit 52cd797

Please sign in to comment.