From fd2df40330df1f39b04f831f41fbcc22bab07a70 Mon Sep 17 00:00:00 2001 From: Tyler Veness Date: Thu, 25 Apr 2024 00:04:31 -0700 Subject: [PATCH] Remove unused formatter specializations (#507) --- include/sleipnir/util/Formatters.hpp | 110 --------------------------- test/src/util/FormattersTest.cpp | 58 -------------- 2 files changed, 168 deletions(-) delete mode 100644 include/sleipnir/util/Formatters.hpp delete mode 100644 test/src/util/FormattersTest.cpp diff --git a/include/sleipnir/util/Formatters.hpp b/include/sleipnir/util/Formatters.hpp deleted file mode 100644 index d22f9900..00000000 --- a/include/sleipnir/util/Formatters.hpp +++ /dev/null @@ -1,110 +0,0 @@ -// Copyright (c) Sleipnir contributors - -#pragma once - -#include - -#include -#include -#include - -#include "sleipnir/autodiff/Variable.hpp" -#include "sleipnir/autodiff/VariableBlock.hpp" -#include "sleipnir/autodiff/VariableMatrix.hpp" - -// FIXME: Doxygen gives internal inconsistency errors: -// scope for class sleipnir::fmt::formatter< Derived, CharT > not found! -// scope for class sleipnir::fmt::formatter< sleipnir::Variable > not found! -// scope for class sleipnir::fmt::formatter< T > not found! - -//! @cond Doxygen_Suppress - -/** - * Formatter for classes derived from Eigen::DenseBase or - * Eigen::SparseCompressedBase. - */ -template - requires std::derived_from> || - std::derived_from> -struct fmt::formatter { - template - constexpr auto parse(ParseContext& ctx) { - return m_underlying.parse(ctx); - } - - template - auto format(const Derived& mat, FmtContext& ctx) const { - auto out = ctx.out(); - - for (int row = 0; row < mat.rows(); ++row) { - for (int col = 0; col < mat.cols(); ++col) { - out = fmt::format_to(out, " "); - out = m_underlying.format(mat.coeff(row, col), ctx); - } - - if (row < mat.rows() - 1) { - out = fmt::format_to(out, "\n"); - } - } - - return out; - } - - private: - fmt::formatter m_underlying; -}; - -/** - * Formatter for sleipnir::Variable. - */ -template <> -struct fmt::formatter { - template - constexpr auto parse(ParseContext& ctx) { - return m_underlying.parse(ctx); - } - - template - auto format(const sleipnir::Variable& variable, FmtContext& ctx) const { - return m_underlying.format(variable.Value(), ctx); - } - - private: - fmt::formatter m_underlying; -}; - -/** - * Formatter for sleipnir::VariableBlock or sleipnir::VariableMatrix. - */ -template - requires std::same_as> || - std::same_as -struct fmt::formatter { - template - constexpr auto parse(ParseContext& ctx) { - return m_underlying.parse(ctx); - } - - template - auto format(const T& mat, FmtContext& ctx) const { - auto out = ctx.out(); - - for (int row = 0; row < mat.Rows(); ++row) { - for (int col = 0; col < mat.Cols(); ++col) { - out = fmt::format_to(out, " "); - out = m_underlying.format(mat(row, col).Value(), ctx); - } - - if (row < mat.Rows() - 1) { - out = fmt::format_to(out, "\n"); - } - } - - return out; - } - - private: - fmt::formatter m_underlying; -}; - -//! @endcond diff --git a/test/src/util/FormattersTest.cpp b/test/src/util/FormattersTest.cpp deleted file mode 100644 index a18d0d76..00000000 --- a/test/src/util/FormattersTest.cpp +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright (c) Sleipnir contributors - -#include - -#include -#include -#include - -TEST_CASE("Formatters - Eigen", "[formatters]") { - Eigen::Matrix A{{0.0, 1.0}, {2.0, 3.0}, {4.0, 5.0}}; - CHECK(fmt::format("{:f}", A) == - " 0.000000 1.000000\n" - " 2.000000 3.000000\n" - " 4.000000 5.000000"); - - Eigen::MatrixXd B{{0.0, 1.0}, {2.0, 3.0}, {4.0, 5.0}}; - CHECK(fmt::format("{:f}", B) == - " 0.000000 1.000000\n" - " 2.000000 3.000000\n" - " 4.000000 5.000000"); - - Eigen::Array2d C{0.0, 1.0}; - CHECK(fmt::format("{:f}", C) == " 0.000000\n 1.000000"); - - Eigen::SparseMatrix D{3, 2}; - std::vector> triplets; - triplets.emplace_back(0, 1, 1.0); - triplets.emplace_back(1, 0, 2.0); - triplets.emplace_back(1, 1, 3.0); - triplets.emplace_back(2, 0, 4.0); - triplets.emplace_back(2, 1, 5.0); - D.setFromTriplets(triplets.begin(), triplets.end()); - CHECK(fmt::format("{:f}", D) == - " 0.000000 1.000000\n" - " 2.000000 3.000000\n" - " 4.000000 5.000000"); -} - -TEST_CASE("Formatters - Variable", "[formatters]") { - CHECK(fmt::format("{:f}", sleipnir::Variable{4.0}) == "4.000000"); -} - -TEST_CASE("Formatters - VariableMatrix", "[formatters]") { - Eigen::Matrix A{{0.0, 1.0}, {2.0, 3.0}, {4.0, 5.0}}; - - sleipnir::VariableMatrix B{3, 2}; - B = A; - CHECK(fmt::format("{:f}", B) == - " 0.000000 1.000000\n" - " 2.000000 3.000000\n" - " 4.000000 5.000000"); - - sleipnir::VariableBlock C{B}; - CHECK(fmt::format("{:f}", C) == - " 0.000000 1.000000\n" - " 2.000000 3.000000\n" - " 4.000000 5.000000"); -}