LCOV - code coverage report
Current view: top level - python - view.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 51 51 100.0 %
Date: 2024-11-17 01:47:58 Functions: 76 94 80.9 %

          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 "scipp/dataset/dataset.h"
       8             : 
       9             : /// Helper to provide equivalent of the `items()` method of a Python dict.
      10             : template <class T> class items_view {
      11             : public:
      12        3786 :   explicit items_view(T &obj) : m_obj(&obj) {}
      13         325 :   auto size() const noexcept { return m_obj->size(); }
      14        3745 :   auto begin() const { return m_obj->items_begin(); }
      15        3745 :   auto end() const { return m_obj->items_end(); }
      16           6 :   auto tostring() const { return to_string(*m_obj); }
      17          14 :   bool operator==(const items_view<T> &other) const {
      18          14 :     return *m_obj == *other.m_obj;
      19             :   }
      20             : 
      21             : private:
      22             :   T *m_obj;
      23             : };
      24             : template <class T> items_view(T &) -> items_view<T>;
      25             : 
      26             : /// Helper to provide equivalent of the `values()` method of a Python dict.
      27             : template <class T> class values_view {
      28             : public:
      29        2682 :   explicit values_view(T &obj) : m_obj(&obj) {}
      30          33 :   auto size() const noexcept { return m_obj->size(); }
      31        2597 :   auto begin() const {
      32             :     if constexpr (std::is_same_v<typename T::mapped_type, scipp::DataArray>)
      33          69 :       return m_obj->begin();
      34             :     else
      35        2528 :       return m_obj->values_begin();
      36             :   }
      37        2625 :   auto end() const {
      38             :     if constexpr (std::is_same_v<typename T::mapped_type, scipp::DataArray>)
      39          73 :       return m_obj->end();
      40             :     else
      41        2552 :       return m_obj->values_end();
      42             :   }
      43          20 :   auto tostring() const {
      44          20 :     std::stringstream ss;
      45          20 :     ss << "<scipp.Dict.values>";
      46          48 :     for (auto it = this->begin(); it != this->end(); ++it)
      47          28 :       ss << "\n" << to_string(*it);
      48          40 :     return ss.str();
      49          20 :   }
      50             :   // No operator== here. As with dict, values do not support comparison
      51             :   // because it is not clear how to handle item order.
      52             : 
      53             : private:
      54             :   T *m_obj;
      55             : };
      56             : template <class T> values_view(T &) -> values_view<T>;
      57             : 
      58             : /// Helper to provide equivalent of the `keys()` method of a Python dict.
      59             : template <class T> class keys_view {
      60             : public:
      61        1553 :   explicit keys_view(T &obj) : m_obj(&obj) {}
      62          13 :   auto size() const noexcept { return m_obj->size(); }
      63         554 :   auto begin() const { return m_obj->keys_begin(); }
      64         554 :   auto end() const { return m_obj->keys_end(); }
      65          46 :   auto tostring() const { return dict_keys_to_string(*m_obj); }
      66         504 :   bool operator==(const keys_view<T> &other) const {
      67        1008 :     return std::is_permutation(m_obj->keys_begin(), m_obj->keys_end(),
      68         504 :                                other.m_obj->keys_begin(),
      69        1512 :                                other.m_obj->keys_end());
      70             :   }
      71             : 
      72             : private:
      73             :   T *m_obj;
      74             : };
      75             : template <class T> keys_view(T &) -> keys_view<T>;
      76             : 
      77        2104 : static constexpr auto dim_to_str = [](auto &&dim) -> decltype(auto) {
      78        2104 :   return dim.name();
      79             : };
      80             : 
      81             : /// Helper to provide equivalent of the `keys()` method of a Python dict.
      82             : template <class T> class str_keys_view {
      83             : public:
      84        3654 :   explicit str_keys_view(T &obj) : m_obj(&obj) {}
      85          43 :   auto size() const noexcept { return m_obj->size(); }
      86        1901 :   auto begin() const { return m_obj->keys_begin().transform(dim_to_str); }
      87        1901 :   auto end() const { return m_obj->keys_end().transform(dim_to_str); }
      88         102 :   auto tostring() const { return dict_keys_to_string(*m_obj); }
      89         884 :   bool operator==(const str_keys_view<T> &other) const {
      90        1768 :     return std::is_permutation(m_obj->keys_begin(), m_obj->keys_end(),
      91         884 :                                other.m_obj->keys_begin(),
      92        2652 :                                other.m_obj->keys_end());
      93             :   }
      94             : 
      95             : private:
      96             :   T *m_obj;
      97             : };
      98             : template <class T> str_keys_view(T &) -> str_keys_view<T>;
      99             : 
     100       10986 : static constexpr auto item_to_str = [](const auto &item) {
     101       10986 :   return std::pair(item.first.name(), item.second);
     102             : };
     103             : 
     104             : /// Helper to provide equivalent of the `items()` method of a Python dict.
     105             : template <class T> class str_items_view {
     106             : public:
     107        6795 :   explicit str_items_view(T &obj) : m_obj(&obj) {}
     108         273 :   auto size() const noexcept { return m_obj->size(); }
     109        6681 :   auto begin() const { return m_obj->items_begin().transform(item_to_str); }
     110        6681 :   auto end() const { return m_obj->items_end().transform(item_to_str); }
     111          14 :   auto tostring() const { return to_string(*m_obj); }
     112          42 :   bool operator==(const str_items_view<T> &other) const {
     113          42 :     return *m_obj == *other.m_obj;
     114             :   }
     115             : 
     116             : private:
     117             :   T *m_obj;
     118             : };
     119             : template <class T> str_items_view(T &) -> str_items_view<T>;

Generated by: LCOV version 1.14