LCOV - code coverage report
Current view: top level - variable - variable_factory.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 45 47 95.7 %
Date: 2024-04-28 01:25:40 Functions: 17 18 94.4 %

          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             : #include "scipp/variable/variable_factory.h"
       6             : 
       7             : namespace scipp::variable {
       8             : 
       9     1376218 : DType VariableFactory::bin_dtype(const parent_list &vars) const noexcept {
      10             :   // First pass: Find, e.g., bucket<DataArray>, since if we have such an input
      11             :   // we need to keep metadata in the output. Note that we would also like to
      12             :   // prioritize bucket<Dataset> higher than bucket<DataArray>, but the former
      13             :   // is not supported in binary operations, so this is not relevant right now.
      14     1376218 :   if (const auto it = std::find_if(vars.begin(), vars.end(),
      15     2007265 :                                    [](const Variable &v) {
      16     2023674 :                                      return variable::is_bins(v) &&
      17       16409 :                                             v.dtype() !=
      18     2007265 :                                                 dtype<bucket<Variable>>;
      19             :                                    });
      20     1376218 :       it != vars.end())
      21          44 :     return it->get().dtype();
      22             :   // Second pass: Find bucket<Variable> (or other)
      23     1376174 :   if (const auto it =
      24     1376174 :           std::find_if(vars.begin(), vars.end(),
      25     2005713 :                        [](const auto &v) { return variable::is_bins(v); });
      26     1376174 :       it != vars.end())
      27       15385 :     return it->get().dtype();
      28             :   // Not binned data
      29     1360789 :   return dtype<void>;
      30             : }
      31             : 
      32         250 : void VariableFactory::emplace(const DType key,
      33             :                               std::unique_ptr<AbstractVariableMaker> maker) {
      34         250 :   m_makers.emplace(key, std::move(maker));
      35         250 : }
      36             : 
      37           0 : bool VariableFactory::contains(const DType key) const noexcept {
      38           0 :   return m_makers.find(key) != m_makers.end();
      39             : }
      40    13775950 : bool VariableFactory::is_bins(const Variable &var) const {
      41    13775950 :   return m_makers.at(var.dtype())->is_bins();
      42             : }
      43             : 
      44          12 : Dim VariableFactory::elem_dim(const Variable &var) const {
      45          12 :   return m_makers.at(var.dtype())->elem_dim(var);
      46             : }
      47             : 
      48    11188514 : DType VariableFactory::elem_dtype(const Variable &var) const {
      49    11188514 :   return m_makers.at(var.dtype())->elem_dtype(var);
      50             : }
      51             : 
      52     3164929 : units::Unit VariableFactory::elem_unit(const Variable &var) const {
      53     3164929 :   return m_makers.at(var.dtype())->elem_unit(var);
      54             : }
      55             : 
      56      502029 : void VariableFactory::expect_can_set_elem_unit(const Variable &var,
      57             :                                                const units::Unit &u) const {
      58      502029 :   m_makers.at(var.dtype())->expect_can_set_elem_unit(var, u);
      59      502016 : }
      60             : 
      61      502050 : void VariableFactory::set_elem_unit(Variable &var, const units::Unit &u) const {
      62      502050 :   m_makers.at(var.dtype())->set_elem_unit(var, u);
      63      502050 : }
      64             : 
      65           5 : bool VariableFactory::has_masks(const Variable &var) const {
      66           5 :   return m_makers.at(var.dtype())->has_masks(var);
      67             : }
      68             : 
      69     6737273 : bool VariableFactory::has_variances(const Variable &var) const {
      70     6737273 :   return m_makers.at(var.dtype())->has_variances(var);
      71             : }
      72             : 
      73      285974 : Variable VariableFactory::empty_like(const Variable &prototype,
      74             :                                      const std::optional<Dimensions> &shape,
      75             :                                      const Variable &sizes) {
      76      571933 :   return m_makers.at(prototype.dtype())->empty_like(prototype, shape, sizes);
      77             : }
      78             : 
      79       41542 : Variable VariableFactory::apply_event_masks(const Variable &var,
      80             :                                             const FillValue fill) const {
      81       83084 :   return m_makers.at(var.dtype())->apply_event_masks(var, fill);
      82             : }
      83             : 
      84          29 : Variable VariableFactory::irreducible_event_mask(const Variable &var) const {
      85          58 :   return m_makers.at(var.dtype())->irreducible_event_mask(var);
      86             : }
      87             : 
      88    37574778 : VariableFactory &variableFactory() {
      89    37574778 :   static VariableFactory factory;
      90    37574778 :   return factory;
      91             : }
      92             : 
      93     6036445 : bool is_bins(const Variable &var) { return variableFactory().is_bins(var); }
      94             : 
      95             : } // namespace scipp::variable

Generated by: LCOV version 1.14