LCOV - code coverage report
Current view: top level - elsa/storage - Span.h (source / functions) Hit Total Coverage
Test: coverage-all.lcov Lines: 49 55 89.1 %
Date: 2024-05-16 04:22:26 Functions: 77 92 83.7 %

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include "ContiguousStorage.h"
       4             : #include "TypeCasts.hpp"
       5             : 
       6             : #include <cstddef>
       7             : #include <iterator>
       8             : #include <type_traits>
       9             : 
      10             : namespace elsa
      11             : {
      12             :     template <class T>
      13             :     class ContiguousStorageView
      14             :     {
      15             :     public:
      16             :         using element_type = T;
      17             :         using value_type = typename std::remove_cv<T>::type;
      18             : 
      19             :         using size_type = typename ContiguousStorage<element_type>::size_type;
      20             :         using difference_type = typename ContiguousStorage<element_type>::difference_type;
      21             : 
      22             :         using pointer = typename ContiguousStorage<element_type>::pointer;
      23             :         using const_pointer = typename ContiguousStorage<element_type>::const_pointer;
      24             : 
      25             :         using reference = typename ContiguousStorage<element_type>::reference;
      26             :         using const_reference = typename ContiguousStorage<element_type>::const_reference;
      27             : 
      28             :         using iterator = typename ContiguousStorage<element_type>::iterator;
      29             :         using const_iterator = typename ContiguousStorage<element_type>::const_iterator;
      30             : 
      31             :         ContiguousStorageView(ContiguousStorage<T>& storage) noexcept;
      32             : 
      33             :         ContiguousStorageView(ContiguousStorage<T>& storage, size_type start,
      34             :                               size_type end) noexcept;
      35             : 
      36             :         ContiguousStorageView(const ContiguousStorageView<T>& other)
      37             :             : ref_(other.ref_), startIdx_(other.startIdx_), endIdx_(other.endIdx_)
      38      289937 :         {
      39      289937 :         }
      40             :         ContiguousStorageView& operator=(const ContiguousStorageView<T>& other)
      41           0 :         {
      42           0 :             ref_ = other.ref_;
      43           0 :             startIdx_ = other.startIdx_;
      44           0 :             endIdx_ = other.endIdx_;
      45           0 :             return *this;
      46           0 :         }
      47             : 
      48             :         ContiguousStorageView(ContiguousStorageView<T>&& storage) noexcept = default;
      49             :         ContiguousStorageView& operator=(ContiguousStorageView<T>&& storage) noexcept = default;
      50             : 
      51             :         template <typename InputIterator>
      52             :         void assign(InputIterator first, InputIterator last);
      53             : 
      54             :         bool empty() const noexcept;
      55             :         size_type size() const noexcept;
      56             :         size_type offset() const noexcept;
      57             : 
      58             :         ContiguousStorage<T>& storage() noexcept;
      59             :         const ContiguousStorage<T>& storage() const noexcept;
      60             : 
      61             :         reference operator[](size_type idx) noexcept;
      62             :         const_reference operator[](size_type idx) const noexcept;
      63             : 
      64             :         pointer data() noexcept;
      65             :         const_pointer data() const noexcept;
      66             : 
      67             :         iterator begin() noexcept;
      68             :         iterator end() noexcept;
      69             : 
      70             :         const_iterator begin() const noexcept;
      71             :         const_iterator end() const noexcept;
      72             : 
      73             :         const_iterator cbegin() const noexcept;
      74             :         const_iterator cend() const noexcept;
      75             : 
      76             :         reference front() noexcept;
      77             :         const_reference front() const noexcept;
      78             : 
      79             :         reference back() noexcept;
      80             :         const_reference back() const noexcept;
      81             : 
      82             :     private:
      83             :         ContiguousStorage<T>& ref_;
      84             :         size_type startIdx_;
      85             :         size_type endIdx_;
      86             :     };
      87             : 
      88             :     template <class T>
      89             :     ContiguousStorageView<T>::ContiguousStorageView(ContiguousStorage<T>& storage) noexcept
      90             :         : ContiguousStorageView(storage, 0, storage.size())
      91             :     {
      92             :     }
      93             : 
      94             :     template <class T>
      95             :     ContiguousStorageView<T>::ContiguousStorageView(ContiguousStorage<T>& storage, size_type start,
      96             :                                                     size_type end) noexcept
      97             :         : ref_(storage), startIdx_(start), endIdx_(end)
      98       74553 :     {
      99       74553 :     }
     100             : 
     101             :     template <class T>
     102             :     template <typename InputIterator>
     103             :     void ContiguousStorageView<T>::assign(InputIterator first, InputIterator last)
     104        1427 :     {
     105        1427 :         thrust::copy(first, last, begin());
     106        1427 :     }
     107             : 
     108             :     template <class T>
     109             :     bool ContiguousStorageView<T>::empty() const noexcept
     110             :     {
     111             :         return size() == 0;
     112             :     }
     113             : 
     114             :     template <class T>
     115             :     typename ContiguousStorageView<T>::size_type ContiguousStorageView<T>::size() const noexcept
     116       49530 :     {
     117       49530 :         return endIdx_ - startIdx_;
     118       49530 :     }
     119             : 
     120             :     template <class T>
     121             :     typename ContiguousStorageView<T>::size_type ContiguousStorageView<T>::offset() const noexcept
     122       72835 :     {
     123       72835 :         return startIdx_;
     124       72835 :     }
     125             : 
     126             :     template <class T>
     127             :     ContiguousStorage<T>& ContiguousStorageView<T>::storage() noexcept
     128      120483 :     {
     129      120483 :         return ref_;
     130      120483 :     }
     131             : 
     132             :     template <class T>
     133             :     const ContiguousStorage<T>& ContiguousStorageView<T>::storage() const noexcept
     134        4330 :     {
     135        4330 :         return ref_;
     136        4330 :     }
     137             : 
     138             :     template <class T>
     139             :     typename ContiguousStorageView<T>::pointer ContiguousStorageView<T>::data() noexcept
     140       14515 :     {
     141       14515 :         return storage().data() + static_cast<difference_type>(startIdx_);
     142       14515 :     }
     143             : 
     144             :     template <class T>
     145             :     typename ContiguousStorageView<T>::const_pointer ContiguousStorageView<T>::data() const noexcept
     146         185 :     {
     147         185 :         return storage().data() + static_cast<difference_type>(startIdx_);
     148         185 :     }
     149             : 
     150             :     template <class T>
     151             :     typename ContiguousStorageView<T>::reference
     152             :         ContiguousStorageView<T>::operator[](size_type idx) noexcept
     153       14515 :     {
     154       14515 :         return data()[asSigned(idx)];
     155       14515 :     }
     156             : 
     157             :     template <class T>
     158             :     typename ContiguousStorageView<T>::const_reference
     159             :         ContiguousStorageView<T>::operator[](size_type idx) const noexcept
     160         184 :     {
     161         184 :         return data()[asSigned(idx)];
     162         184 :     }
     163             : 
     164             :     template <class T>
     165             :     typename ContiguousStorageView<T>::const_reference
     166             :         ContiguousStorageView<T>::front() const noexcept
     167             :     {
     168             :         return data()[0];
     169             :     }
     170             : 
     171             :     template <class T>
     172             :     typename ContiguousStorageView<T>::reference ContiguousStorageView<T>::front() noexcept
     173             :     {
     174             :         return data()[0];
     175             :     }
     176             : 
     177             :     template <class T>
     178             :     typename ContiguousStorageView<T>::reference ContiguousStorageView<T>::back() noexcept
     179             :     {
     180             :         return data()[size() - 1];
     181             :     }
     182             : 
     183             :     template <class T>
     184             :     typename ContiguousStorageView<T>::const_reference
     185             :         ContiguousStorageView<T>::back() const noexcept
     186             :     {
     187             :         return data()[size() - 1];
     188             :     }
     189             : 
     190             :     template <class T>
     191             :     typename ContiguousStorageView<T>::iterator ContiguousStorageView<T>::begin() noexcept
     192       35655 :     {
     193       35655 :         return thrust::next(storage().begin(), static_cast<difference_type>(startIdx_));
     194       35655 :     }
     195             : 
     196             :     template <class T>
     197             :     typename ContiguousStorageView<T>::iterator ContiguousStorageView<T>::end() noexcept
     198       34347 :     {
     199       34347 :         return thrust::next(storage().begin(), static_cast<difference_type>(endIdx_));
     200       34347 :     }
     201             : 
     202             :     template <class T>
     203             :     typename ContiguousStorageView<T>::const_iterator
     204             :         ContiguousStorageView<T>::begin() const noexcept
     205         252 :     {
     206         252 :         return cbegin();
     207         252 :     }
     208             : 
     209             :     template <class T>
     210             :     typename ContiguousStorageView<T>::const_iterator ContiguousStorageView<T>::end() const noexcept
     211         252 :     {
     212         252 :         return cend();
     213         252 :     }
     214             : 
     215             :     template <class T>
     216             :     typename ContiguousStorageView<T>::const_iterator
     217             :         ContiguousStorageView<T>::cbegin() const noexcept
     218        2106 :     {
     219        2106 :         return thrust::next(storage().cbegin(), static_cast<difference_type>(startIdx_));
     220        2106 :     }
     221             : 
     222             :     template <class T>
     223             :     typename ContiguousStorageView<T>::const_iterator
     224             :         ContiguousStorageView<T>::cend() const noexcept
     225        2040 :     {
     226        2040 :         return thrust::next(storage().cbegin(), static_cast<difference_type>(endIdx_));
     227        2040 :     }
     228             : } // namespace elsa

Generated by: LCOV version 1.14