LCOV - code coverage report
Current view: top level - units - dim.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 32 35 91.4 %
Date: 2024-04-28 01:25:40 Functions: 5 5 100.0 %

          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 <limits>
       6             : #include <mutex>
       7             : #include <shared_mutex>
       8             : #include <stdexcept>
       9             : #include <unordered_map>
      10             : 
      11             : #include "scipp/common/index.h"
      12             : #include "scipp/units/dim.h"
      13             : 
      14             : namespace scipp::units {
      15             : 
      16             : namespace {
      17      900635 : const auto &builtin_ids() {
      18             :   static std::unordered_map<std::string, Dim::Id> ids{
      19             :       {"<invalid>", Dim::Invalid},
      20             :       {"<none>", Dim::None},
      21             :       {"<internal_structure_component>", Dim::InternalStructureComponent},
      22             :       {"<internal_structure_row>", Dim::InternalStructureRow},
      23             :       {"<internal_structure_column>", Dim::InternalStructureColumn},
      24             :       {"<internal_histogram>", Dim::InternalHistogram},
      25             :       {"<internal_sort>", Dim::InternalSort},
      26             :       {"<internal_accumulate>", Dim::InternalAccumulate},
      27             :       {"energy", Dim::Energy},
      28             :       {"event", Dim::Event},
      29             :       {"group", Dim::Group},
      30             :       {"position", Dim::Position},
      31             :       {"row", Dim::Row},
      32             :       {"temperature", Dim::Temperature},
      33             :       {"time", Dim::Time},
      34             :       {"wavelength", Dim::Wavelength},
      35             :       {"x", Dim::X},
      36             :       {"y", Dim::Y},
      37      900749 :       {"z", Dim::Z}};
      38      900407 :   return ids;
      39             : }
      40             : 
      41     1036367 : auto &custom_ids() {
      42     1036367 :   static std::unordered_map<std::string, Dim::Id> ids;
      43     1035160 :   return ids;
      44             : }
      45             : 
      46             : std::shared_mutex mutex;
      47             : } // namespace
      48             : 
      49      409570 : Dim::Dim(const std::string &label) {
      50      409570 :   if (const auto it = builtin_ids().find(label); it != builtin_ids().end()) {
      51       34250 :     m_id = it->second;
      52       34250 :     return;
      53             :   }
      54      372646 :   std::shared_lock read_lock(mutex);
      55      373464 :   if (const auto it = custom_ids().find(label); it != custom_ids().end()) {
      56      367826 :     m_id = it->second;
      57      370116 :     return;
      58             :   }
      59        2135 :   read_lock.unlock();
      60        2135 :   const std::unique_lock write_lock(mutex);
      61        2135 :   const auto id = scipp::size(custom_ids()) + 1000;
      62        2135 :   if (id > std::numeric_limits<std::underlying_type<Id>::type>::max())
      63           0 :     throw std::runtime_error(
      64           0 :         "Exceeded maximum number of different dimension labels.");
      65        2135 :   m_id = static_cast<Id>(id);
      66        2135 :   custom_ids()[label] = m_id;
      67      372251 : }
      68             : 
      69      382586 : std::string Dim::name() const {
      70      382586 :   if (static_cast<int64_t>(m_id) < 1000)
      71      614566 :     for (const auto &item : builtin_ids())
      72      614566 :       if (item.second == m_id)
      73       86267 :         return item.first;
      74      296319 :   const std::shared_lock read_lock(mutex);
      75    27881030 :   for (const auto &item : custom_ids())
      76    27879755 :     if (item.second == m_id)
      77      294894 :       return item.first;
      78           0 :   return "unreachable"; // throw or terminate?
      79      296143 : }
      80             : 
      81       24851 : std::string to_string(const Dim dim) { return dim.name(); }
      82             : 
      83             : } // namespace scipp::units

Generated by: LCOV version 1.14