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 1439625 : 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 1439625 : if (const auto it = std::find_if(vars.begin(), vars.end(), 15 2092505 : [](const Variable &v) { 16 2108920 : return variable::is_bins(v) && 17 16415 : v.dtype() != 18 2092505 : dtype<bucket<Variable>>; 19 : }); 20 1439625 : it != vars.end()) 21 50 : return it->get().dtype(); 22 : // Second pass: Find bucket<Variable> (or other) 23 1439575 : if (const auto it = 24 1439575 : std::find_if(vars.begin(), vars.end(), 25 2090947 : [](const auto &v) { return variable::is_bins(v); }); 26 1439575 : it != vars.end()) 27 15385 : return it->get().dtype(); 28 : // Not binned data 29 1424190 : 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 14234128 : bool VariableFactory::is_bins(const Variable &var) const { 41 14234128 : 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 11468656 : DType VariableFactory::elem_dtype(const Variable &var) const { 49 11468656 : return m_makers.at(var.dtype())->elem_dtype(var); 50 : } 51 : 52 3266321 : units::Unit VariableFactory::elem_unit(const Variable &var) const { 53 3266321 : return m_makers.at(var.dtype())->elem_unit(var); 54 : } 55 : 56 508021 : void VariableFactory::expect_can_set_elem_unit(const Variable &var, 57 : const units::Unit &u) const { 58 508021 : m_makers.at(var.dtype())->expect_can_set_elem_unit(var, u); 59 508008 : } 60 : 61 508042 : void VariableFactory::set_elem_unit(Variable &var, const units::Unit &u) const { 62 508042 : m_makers.at(var.dtype())->set_elem_unit(var, u); 63 508042 : } 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 7017181 : bool VariableFactory::has_variances(const Variable &var) const { 70 7017181 : return m_makers.at(var.dtype())->has_variances(var); 71 : } 72 : 73 290215 : Variable VariableFactory::empty_like(const Variable &prototype, 74 : const std::optional<Dimensions> &shape, 75 : const Variable &sizes) { 76 580415 : return m_makers.at(prototype.dtype())->empty_like(prototype, shape, sizes); 77 : } 78 : 79 42605 : Variable VariableFactory::apply_event_masks(const Variable &var, 80 : const FillValue fill) const { 81 85210 : return m_makers.at(var.dtype())->apply_event_masks(var, fill); 82 : } 83 : 84 31 : Variable VariableFactory::irreducible_event_mask(const Variable &var) const { 85 62 : return m_makers.at(var.dtype())->irreducible_event_mask(var); 86 : } 87 : 88 38775095 : VariableFactory &variableFactory() { 89 38775095 : static VariableFactory factory; 90 38775095 : return factory; 91 : } 92 : 93 6234067 : bool is_bins(const Variable &var) { return variableFactory().is_bins(var); } 94 : 95 : } // namespace scipp::variable