LCOV - code coverage report
Current view: top level - core - dtype.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 23 29 79.3 %
Date: 2024-12-01 01:56:34 Functions: 8 10 80.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: BSD-3-Clause
       2             : // Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
       3             : #include <ostream>
       4             : 
       5             : #include "scipp/core/dtype.h"
       6             : #include "scipp/core/eigen.h"
       7             : #include "scipp/core/except.h"
       8             : #include "scipp/core/spatial_transforms.h"
       9             : #include "scipp/core/string.h"
      10             : 
      11             : namespace scipp::core {
      12             : 
      13        3673 : bool is_int(DType tp) { return tp == dtype<int32_t> || tp == dtype<int64_t>; }
      14             : 
      15        1648 : bool is_float(DType tp) { return tp == dtype<float> || tp == dtype<double>; }
      16             : 
      17        3085 : bool is_fundamental(DType tp) {
      18        3085 :   return is_int(tp) || is_float(tp) || tp == dtype<bool>;
      19             : }
      20             : 
      21         444 : bool is_total_orderable(DType tp) {
      22         444 :   return is_fundamental(tp) || tp == dtype<time_point>;
      23             : }
      24             : 
      25             : namespace {
      26           0 : template <class... Ts> bool is_span_impl(DType tp) {
      27           0 :   return (((tp == dtype<scipp::span<Ts>>) ||
      28           0 :            (tp == dtype<scipp::span<const Ts>>)) ||
      29           0 :           ...);
      30             : }
      31             : } // namespace
      32             : 
      33           0 : bool is_span(DType tp) {
      34           0 :   return is_span_impl<double, float, int64_t, int32_t, bool, time_point>(tp);
      35             : }
      36             : 
      37          31 : std::ostream &operator<<(std::ostream &os, const DType &dtype) {
      38          31 :   return os << to_string(dtype);
      39             : }
      40             : 
      41             : auto register_dtype_name_void(
      42             :     (core::dtypeNameRegistry().emplace(dtype<void>, "void"), 0));
      43             : 
      44             : namespace {
      45             : template <class T, class... Ts> constexpr auto inner_lut() {
      46             :   return std::array{std::pair{dtype<Ts>, dtype<std::common_type_t<T, Ts>>}...};
      47             : }
      48             : 
      49             : template <class... Ts> constexpr auto outer_lut() {
      50             :   return std::array{std::pair{dtype<Ts>, inner_lut<Ts, Ts...>()}...};
      51             : }
      52             : } // namespace
      53             : 
      54       47623 : DType common_type(const DType &a, const DType &b) {
      55       47623 :   if (a == b)
      56       47591 :     return a;
      57          32 :   const auto lut = outer_lut<double, float, int64_t, int32_t>();
      58          32 :   const auto it = std::find_if(lut.begin(), lut.end(), [&](const auto &pair) {
      59          89 :     return pair.first == a;
      60             :   });
      61          32 :   if (it == lut.end())
      62           1 :     throw except::TypeError("'common_type' does not support dtype " +
      63           2 :                             to_string(a));
      64             :   const auto it2 =
      65          31 :       std::find_if(it->second.begin(), it->second.end(),
      66          60 :                    [&](const auto &pair) { return pair.first == b; });
      67          31 :   if (it2 == it->second.end())
      68           1 :     throw except::TypeError("'common_type' does not support dtype " +
      69           2 :                             to_string(b));
      70          30 :   return it2->second;
      71             : }
      72             : 
      73             : } // namespace scipp::core

Generated by: LCOV version 1.14