LCOV - code coverage report
Current view: top level - dtwc/types - Index.hpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 14 14 100.0 %
Date: 2024-09-07 20:53:22 Functions: 10 10 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file Index.hpp
       3             :  * @brief A basic Index class for Range class
       4             :  * @author Volkan Kumtepeli
       5             :  * @author Becky Perriment
       6             :  * @date 18 Aug 2022
       7             :  */
       8             : 
       9             : #pragma once
      10             : 
      11             : #include <iterator>
      12             : 
      13             : namespace dtwc {
      14             : 
      15             : class Index
      16             : {
      17             :   /*
      18             :   Adapted from: https://stackoverflow.com/questions/61208870/how-to-write-a-random-access-custom-iterator-that-can-be-used-with-stl-algorithm
      19             : */
      20             :   size_t ptr{};
      21             : 
      22             : public:
      23             :   using iterator_category = std::random_access_iterator_tag;
      24             :   using value_type = size_t;
      25             :   using reference = size_t;
      26             :   using pointer = size_t;
      27             :   using difference_type = size_t;
      28             : 
      29             :   Index() = default;
      30          22 :   Index(size_t ptr) : ptr(ptr) {}
      31             : 
      32          16 :   reference operator*() const { return ptr; }
      33             :   pointer operator->() const { return ptr; }
      34             : 
      35           4 :   Index &operator++()
      36             :   {
      37           4 :     ptr++;
      38           4 :     return *this;
      39             :   }
      40           1 :   Index &operator--()
      41             :   {
      42           1 :     ptr--;
      43           1 :     return *this;
      44             :   }
      45             : 
      46           1 :   difference_type operator-(const Index &it) const { return this->ptr - it.ptr; }
      47             : 
      48           2 :   Index operator+(const difference_type &diff) const { return Index(ptr + diff); }
      49           1 :   Index operator-(const difference_type &diff) const { return Index(ptr - diff); }
      50             : 
      51           1 :   reference operator[](const difference_type &offset) const { return *(*this + offset); }
      52             : 
      53           2 :   bool operator==(const Index &it) const { return this->ptr == it.ptr; }
      54           4 :   bool operator!=(const Index &it) const { return this->ptr != it.ptr; }
      55             :   bool operator<(const Index &it) const { return this->ptr < it.ptr; }
      56             :   bool operator>(const Index &it) const { return this->ptr > it.ptr; }
      57             :   bool operator>=(const Index &it) const { return !(this->ptr < it.ptr); }
      58             :   bool operator<=(const Index &it) const { return !(this->ptr > it.ptr); }
      59             : };
      60             : 
      61             : } // namespace dtwc

Generated by: LCOV version 1.14