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 <vector> 8 : 9 : #include "scipp-core_export.h" 10 : #include "scipp/core/except.h" 11 : #include "scipp/units/dim.h" 12 : 13 : namespace scipp::core { 14 : 15 : namespace detail { 16 : template <class T> 17 2898 : [[nodiscard]] T rename_dims(const T &obj, 18 : const std::vector<std::pair<Dim, Dim>> &names, 19 : const bool fail_on_unknown) { 20 2898 : auto out(obj); 21 6549 : for (const auto &[from, to] : names) 22 3653 : if (out.contains(from)) 23 3318 : out.replace_key(from, to); 24 335 : else if (fail_on_unknown) 25 1 : throw except::DimensionError( 26 : "Cannot rename dimension " + to_string(from) + 27 : " since it is not contained in the input dimensions " + 28 : to_string(obj) + "."); 29 2896 : return out; 30 2 : } 31 : } // namespace detail 32 : 33 : } // namespace scipp::core