LCOV - code coverage report
Current view: top level - core/include/scipp/core - array_to_string.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 28 45 62.2 %
Date: 2024-04-28 01:25:40 Functions: 25 71 35.2 %

          Line data    Source code
       1             : // SPDX-License-Identifier: BSD-3-Clause
       2             : // Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
       3             : /// @file
       4             : /// @author Simon Heybrock
       5             : #pragma once
       6             : 
       7             : #include <optional>
       8             : #include <string>
       9             : 
      10             : #include <Eigen/Core>
      11             : #include <Eigen/Geometry>
      12             : 
      13             : #include "scipp/units/unit.h"
      14             : 
      15             : #include "scipp/core/eigen.h"
      16             : #include "scipp/core/string.h"
      17             : 
      18             : namespace scipp::core {
      19             : 
      20             : template <class T>
      21             : std::string
      22             : array_to_string(const T &arr,
      23             :                 const std::optional<units::Unit> &unit = std::nullopt);
      24             : 
      25             : template <class T>
      26             : std::string
      27        4834 : element_to_string(const T &item,
      28             :                   const std::optional<units::Unit> &unit = std::nullopt) {
      29             :   using core::to_string;
      30             :   using std::to_string;
      31             :   if constexpr (std::is_same_v<T, std::string>) {
      32          32 :     if (item.length() > 80) {
      33           0 :       return {'"' + item.substr(0, 77) + "...\", "};
      34             :     }
      35          32 :     return {'"' + item + "\", "};
      36             :   } else if constexpr (std::is_same_v<T, bool>)
      37          76 :     return core::to_string(item) + ", ";
      38             :   else if constexpr (std::is_same_v<T, scipp::core::time_point>) {
      39          24 :     return core::to_string(to_iso_date(item, unit.value())) + ", ";
      40             :   } else if constexpr (std::is_same_v<T, Eigen::Vector3d>) {
      41           0 :     std::stringstream ss;
      42           0 :     ss << "(" << item[0] << ", " << item[1] << ", " << item[2] << "), ";
      43           0 :     return ss.str();
      44           0 :   } else if constexpr (std::is_same_v<T, Eigen::Matrix3d>)
      45           0 :     return {"(" + element_to_string(Eigen::Vector3d(item.row(0))) + ", " +
      46           0 :             element_to_string(Eigen::Vector3d(item.row(1))) + ", " +
      47           0 :             element_to_string(Eigen::Vector3d(item.row(2))) + "), "};
      48             :   else if constexpr (std::is_same_v<T, Eigen::Affine3d>) {
      49           0 :     return element_to_string(item.matrix());
      50             :   } else if constexpr (std::is_same_v<T, scipp::core::Quaternion>) {
      51           0 :     std::stringstream ss;
      52           0 :     ss << '(' << item.quat().w();
      53           0 :     ss.setf(std::ios::showpos);
      54           0 :     ss << item.quat().x() << 'i' << item.quat().y() << 'j' << item.quat().z()
      55           0 :        << "k), ";
      56           0 :     return ss.str();
      57           0 :   } else if constexpr (std::is_same_v<T, scipp::core::Translation>)
      58           0 :     return element_to_string(item.vector());
      59             :   else {
      60        4713 :     std::stringstream ss;
      61        4713 :     ss << item << ", ";
      62        9426 :     return ss.str();
      63        4713 :   }
      64             : }
      65             : 
      66             : template <class T>
      67         283 : std::string scalar_array_to_string(const T &arr,
      68             :                                    const std::optional<units::Unit> &unit) {
      69         283 :   auto s = element_to_string(arr[0], unit);
      70         564 :   return s.substr(0, s.size() - 2);
      71         282 : }
      72             : 
      73             : template <class T>
      74        1616 : std::string array_to_string(const T &arr,
      75             :                             const std::optional<units::Unit> &unit) {
      76        1616 :   const auto size = scipp::size(arr);
      77        1616 :   if (size == 0)
      78         248 :     return "[]";
      79        1368 :   std::string s = "[";
      80        5918 :   for (scipp::index i = 0; i < size; ++i) {
      81        4551 :     constexpr scipp::index n = 2;
      82        4551 :     if (i == n && size > 2 * n) {
      83         942 :       s += "..., ";
      84         942 :       i = size - n;
      85             :     }
      86        4551 :     s += element_to_string(arr[i], unit);
      87             :   }
      88        1367 :   s.resize(s.size() > 1 ? s.size() - 2 : 1);
      89        1367 :   s += "]";
      90        1367 :   return s;
      91        1368 : }
      92             : 
      93             : } // namespace scipp::core

Generated by: LCOV version 1.14