LCOV - code coverage report
Current view: top level - core/include/scipp/core - bucket_array_view.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 9 9 100.0 %
Date: 2024-04-28 01:25:40 Functions: 20 30 66.7 %

          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 <boost/iterator/transform_iterator.hpp>
       8             : 
       9             : #include "scipp/core/bucket.h"
      10             : #include "scipp/core/element_array_view.h"
      11             : 
      12             : namespace scipp::core {
      13             : 
      14             : template <class T>
      15             : class bucket_array_view : public ElementArrayView<const scipp::index_pair> {
      16             : public:
      17             :   using value_type = const scipp::index_pair;
      18             :   using base = ElementArrayView<const value_type>;
      19             : 
      20       61935 :   bucket_array_view(const base &buckets, const Dim dim, T &buffer)
      21       61935 :       : base(buckets), m_transform{dim, &buffer} {}
      22             : 
      23       51909 :   auto begin() const {
      24       51909 :     return boost::make_transform_iterator(base::begin(), m_transform);
      25             :   }
      26       51548 :   auto end() const {
      27       51548 :     return boost::make_transform_iterator(base::end(), m_transform);
      28             :   }
      29             : 
      30         722 :   auto operator[](const scipp::index i) const { return *(begin() + i); }
      31             : 
      32             :   auto front() const { return *begin(); }
      33             :   auto back() const { return *(begin() + (size() - 1)); }
      34             : 
      35             :   bool operator==(const bucket_array_view &other) const {
      36             :     if (dims() != other.dims())
      37             :       return false;
      38             :     return std::equal(begin(), end(), other.begin());
      39             :   }
      40             : 
      41             :   template <class T2> bool overlaps(const bucket_array_view<T2> &other) const {
      42             :     if (buffer() == other.buffer())
      43             :       return ElementArrayViewParams::overlaps(other);
      44             :     return false;
      45             :   }
      46             : 
      47             : private:
      48             :   struct make_item {
      49      573881 :     auto operator()(typename bucket_array_view::value_type &range) const {
      50      573881 :       return m_buffer->slice({m_dim, range.first, range.second});
      51             :     }
      52             :     Dim m_dim;
      53             :     T *m_buffer;
      54             :   };
      55             :   make_item m_transform;
      56             : };
      57             : 
      58             : /// Specialization of ElementArrayView for mutable access to bucketed data.
      59             : ///
      60             : /// Iteration returns a mutable view to a slice of the underlying buffer. For
      61             : /// example, a VariableView in case of T=Variable.
      62             : template <class T>
      63             : class ElementArrayView<bucket<T>> : public bucket_array_view<T> {
      64             : public:
      65             :   using value_type = T;
      66             :   using bucket_array_view<T>::bucket_array_view;
      67             : };
      68             : 
      69             : /// Specialization of ElementArrayView for const access to bucketed data.
      70             : ///
      71             : /// Iteration returns a const view to a slice of the underlying buffer. For
      72             : /// example, a VariableConstView in case of T=Variable.
      73             : template <class T>
      74             : class ElementArrayView<const bucket<T>> : public bucket_array_view<const T> {
      75             : public:
      76             :   using value_type = T;
      77             :   using bucket_array_view<const T>::bucket_array_view;
      78             : };
      79             : 
      80             : } // namespace scipp::core

Generated by: LCOV version 1.14