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-04-28 01:25:40 Functions: 75 94 79.8 %

          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        3621 :   explicit items_view(T &obj) : m_obj(&obj) {}
      13         352 :   auto size() const noexcept { return m_obj->size(); }
      14        3580 :   auto begin() const { return m_obj->items_begin(); }
      15        3580 :   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        3407 :   explicit values_view(T &obj) : m_obj(&obj) {}
      30          44 :   auto size() const noexcept { return m_obj->size(); }
      31        3323 :   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        3254 :       return m_obj->values_begin();
      36             :   }
      37        3351 :   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        3278 :       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        1328 :   explicit keys_view(T &obj) : m_obj(&obj) {}
      62          21 :   auto size() const noexcept { return m_obj->size(); }
      63         363 :   auto begin() const { return m_obj->keys_begin(); }
      64         363 :   auto end() const { return m_obj->keys_end(); }
      65          66 :   auto tostring() const { return dict_keys_to_string(*m_obj); }
      66         487 :   bool operator==(const keys_view<T> &other) const {
      67         974 :     return std::is_permutation(m_obj->keys_begin(), m_obj->keys_end(),
      68         487 :                                other.m_obj->keys_begin(),
      69        1461 :                                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        1926 : static constexpr auto dim_to_str = [](auto &&dim) -> decltype(auto) {
      78        1926 :   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        3324 :   explicit str_keys_view(T &obj) : m_obj(&obj) {}
      85          67 :   auto size() const noexcept { return m_obj->size(); }
      86        1639 :   auto begin() const { return m_obj->keys_begin().transform(dim_to_str); }
      87        1639 :   auto end() const { return m_obj->keys_end().transform(dim_to_str); }
      88         146 :   auto tostring() const { return dict_keys_to_string(*m_obj); }
      89         850 :   bool operator==(const str_keys_view<T> &other) const {
      90        1700 :     return std::is_permutation(m_obj->keys_begin(), m_obj->keys_end(),
      91         850 :                                other.m_obj->keys_begin(),
      92        2550 :                                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       10078 : static constexpr auto item_to_str = [](const auto &item) {
     101       10078 :   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        6271 :   explicit str_items_view(T &obj) : m_obj(&obj) {}
     108         290 :   auto size() const noexcept { return m_obj->size(); }
     109        6157 :   auto begin() const { return m_obj->items_begin().transform(item_to_str); }
     110        6157 :   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