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 1443465 : 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 1443465 : if (const auto it = std::find_if(vars.begin(), vars.end(), 15 2094724 : [](const Variable &v) { 16 2111139 : return variable::is_bins(v) && 17 16415 : v.dtype() != 18 2094724 : dtype<bucket<Variable>>; 19 : }); 20 1443465 : it != vars.end()) 21 50 : return it->get().dtype(); 22 : // Second pass: Find bucket<Variable> (or other) 23 1443415 : if (const auto it = 24 1443415 : std::find_if(vars.begin(), vars.end(), 25 2093166 : [](const auto &v) { return variable::is_bins(v); }); 26 1443415 : it != vars.end()) 27 15385 : return it->get().dtype(); 28 : // Not binned data 29 1428030 : 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 14270893 : bool VariableFactory::is_bins(const Variable &var) const { 41 14270893 : 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 11496291 : DType VariableFactory::elem_dtype(const Variable &var) const { 49 11496291 : return m_makers.at(var.dtype())->elem_dtype(var); 50 : } 51 : 52 3274234 : units::Unit VariableFactory::elem_unit(const Variable &var) const { 53 3274234 : return m_makers.at(var.dtype())->elem_unit(var); 54 : } 55 : 56 510027 : void VariableFactory::expect_can_set_elem_unit(const Variable &var, 57 : const units::Unit &u) const { 58 510027 : m_makers.at(var.dtype())->expect_can_set_elem_unit(var, u); 59 510014 : } 60 : 61 510048 : void VariableFactory::set_elem_unit(Variable &var, const units::Unit &u) const { 62 510048 : m_makers.at(var.dtype())->set_elem_unit(var, u); 63 510048 : } 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 7025103 : bool VariableFactory::has_variances(const Variable &var) const { 70 7025103 : return m_makers.at(var.dtype())->has_variances(var); 71 : } 72 : 73 291350 : Variable VariableFactory::empty_like(const Variable &prototype, 74 : const std::optional<Dimensions> &shape, 75 : const Variable &sizes) { 76 582685 : return m_makers.at(prototype.dtype())->empty_like(prototype, shape, sizes); 77 : } 78 : 79 43991 : Variable VariableFactory::apply_event_masks(const Variable &var, 80 : const FillValue fill) const { 81 87982 : 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 38865703 : VariableFactory &variableFactory() { 89 38865703 : static VariableFactory factory; 90 38865703 : return factory; 91 : } 92 : 93 6252869 : bool is_bins(const Variable &var) { return variableFactory().is_bins(var); } 94 : 95 : } // namespace scipp::variable