LCOV - code coverage report
Current view: top level - atermpp/include/mcrl2/atermpp/detail - aterm_list_iterator.h (source / functions) Hit Total Coverage
Test: mcrl2_coverage.info.cleaned Lines: 57 60 95.0 %
Date: 2024-04-26 03:18:02 Functions: 197 249 79.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Author(s): Jan Friso Groote, Wieger Wesselink
       2             : // Copyright: see the accompanying file COPYING or copy at
       3             : // https://github.com/mCRL2org/mCRL2/blob/master/COPYING
       4             : //
       5             : // Distributed under the Boost Software License, Version 1.0.
       6             : // (See accompanying file LICENSE_1_0.txt or copy at
       7             : // http://www.boost.org/LICENSE_1_0.txt)
       8             : //
       9             : /// \file mcrl2/atermpp/detail/aterm_list_iterator.h
      10             : /// \brief Iterator for term_list.
      11             : 
      12             : #ifndef MCRL2_ATERMPP_ATERM_LIST_ITERATOR_H
      13             : #define MCRL2_ATERMPP_ATERM_LIST_ITERATOR_H
      14             : 
      15             : #include "mcrl2/atermpp/detail/global_aterm_pool.h"
      16             : 
      17             : namespace atermpp
      18             : {
      19             : 
      20             : /// \cond INTERNAL_DOCS
      21             : namespace detail
      22             : {
      23             :   template <class Term>
      24             :   class _aterm_list;
      25             : }
      26             : 
      27             : /// \endcond
      28             : 
      29             : /// \brief Iterator for term_list.
      30             : template <typename Term>
      31             : class term_list_iterator 
      32             : {
      33             :     template<class T>
      34             :     friend class term_list;
      35             : 
      36             :   protected:
      37             :     const detail::_aterm_list<Term>* m_list;
      38             : 
      39             :     /// \brief Constructor from an aterm which must be a list.
      40             :     /// \param l A sequence of terms
      41  3594011038 :     term_list_iterator(const detail::_aterm* l)
      42  3594011038 :       : m_list(static_cast<const detail::_aterm_list<Term>*>(l))
      43             :     { 
      44  3594011038 :       assert(l->function()==detail::g_term_pool().as_list()
      45             :              || l->function()==detail::g_term_pool().as_empty_list());
      46  3594011038 :     } 
      47             : 
      48             :   public:
      49             :     typedef Term value_type;
      50             :     typedef Term& reference;
      51             :     typedef Term* pointer;
      52             :     typedef ptrdiff_t difference_type;
      53             :     typedef std::forward_iterator_tag iterator_category;
      54             : 
      55             :     /// \brief Default constructor.
      56           0 :     term_list_iterator()
      57           0 :       : m_list(nullptr)
      58           0 :     {}
      59             : 
      60             :     /// \brief Copy constructor.
      61             :     /// \param other A sequence of terms
      62    78340431 :     term_list_iterator(const term_list_iterator& other)
      63    78340431 :       : m_list(other.m_list)
      64             :     { 
      65    78340431 :     } 
      66             : 
      67             :     /// \brief Assignment
      68             :     /// \param other A sequence of terms
      69     3632173 :     term_list_iterator& operator=(const term_list_iterator& other)
      70             :     { 
      71     3632173 :       m_list=other.m_list;
      72     3632173 :       return *this;
      73             :     } 
      74             : 
      75             :     /// \brief Dereference operator on an iterator
      76  1089192008 :     const Term& operator*() const
      77             :     {
      78  1089192008 :       assert(m_list->function()==detail::g_term_pool().as_list());
      79  1089192008 :       return m_list->head();
      80             :     }
      81             : 
      82             :     /// Arrow operator on an iterator
      83     4480052 :     const Term* operator->() const
      84             :     {
      85     4480052 :       assert(m_list->function()==detail::g_term_pool().as_list());
      86     4480052 :       return &m_list->head();
      87             :     }
      88             :     
      89             :     /// \brief Prefix increment operator on iterator.
      90  2148511264 :     term_list_iterator& operator++()
      91             :     {
      92  2148511264 :       assert(m_list->function() == detail::g_term_pool().as_list());
      93  2148511264 :       m_list = static_cast<detail::_aterm_list<Term>*>(detail::address(m_list->tail()));
      94  2148511264 :       return *this;
      95             :     }
      96             : 
      97             :     /// \brief Postfix increment operator on iterator.
      98     9488492 :     term_list_iterator operator++(int)
      99             :     {
     100     9488492 :       assert(m_list->function() == detail::g_term_pool().as_list());
     101     9488492 :       const term_list_iterator temp = *this;
     102     9488492 :       m_list = static_cast<detail::_aterm_list<Term>*>(detail::address(m_list->tail()));
     103     9488492 :       return temp;
     104             :     }
     105             : 
     106             :     /// \brief Equality of iterators.
     107             :     /// \param other The iterator with which this iterator is compared.
     108             :     /// \return true if the iterators point to the same term_list.
     109    15552442 :     bool operator ==(const term_list_iterator& other) const
     110             :     {
     111    15552442 :       return m_list == other.m_list;
     112             :     }
     113             : 
     114             :     /// \brief Inequality of iterators.
     115             :     /// \param other The iterator with which this iterator is compared.
     116             :     /// \return true if the iterators do not point to the same term_list.
     117  3420957973 :     bool operator !=(const term_list_iterator& other) const
     118             :     {
     119  3420957973 :       return m_list != other.m_list;
     120             :     }
     121             : 
     122             :     /// \brief Comparison of iterators.
     123             :     /// \param other The iterator with which this iterator is compared.
     124             :     /// \return true if the pointer to this termlist is smaller than the other pointer.
     125             :     bool operator <(const term_list_iterator& other) const
     126             :     {
     127             :       return m_list < other.m_list;
     128             :     }
     129             : 
     130             :     /// \brief Comparison of iterators.
     131             :     /// \param other The iterator with which this iterator is compared.
     132             :     /// \return true if the iterators point to the same term_list.
     133             :     bool operator <=(const term_list_iterator& other) const
     134             :     {
     135             :       return m_list <= other.m_list;
     136             :     }
     137             : 
     138             :     /// \brief Comparison of iterators.
     139             :     /// \param other The iterator with which this iterator is compared.
     140             :     /// \return true if the iterators point to the same term_list.
     141             :     bool operator >(const term_list_iterator& other) const
     142             :     {
     143             :       return m_list > other.m_list;
     144             :     }
     145             : 
     146             :     /// \brief Comparison of iterators.
     147             :     /// \param other The iterator with which this iterator is compared.
     148             :     /// \return true if the iterators point to the same term_list.
     149             :     bool operator >=(const term_list_iterator& other) const
     150             :     {
     151             :       return m_list >= other.m_list;
     152             :     }
     153             : 
     154             : };
     155             : 
     156             : /// \brief Reverse iterator for term_list.
     157             : template <typename Term>
     158             : class reverse_term_list_iterator 
     159             : {
     160             :     template<class T>
     161             :     friend class term_list;
     162             : 
     163             :   protected:
     164             :     std::size_t m_position;   // m_position refers one above the position to be deliverd. 
     165             :     std::unique_ptr<detail::_aterm_list<Term> const*[]> m_list_element_references;
     166             : 
     167             :     /// \brief Constructor from an aterm which must be a list.
     168             :     /// \param l A sequence of terms
     169        2595 :     reverse_term_list_iterator(detail::_aterm const* l)
     170        2595 :       : m_position(reinterpret_cast<const detail::_aterm_list<Term>*>(l)->size()),
     171        2595 :         m_list_element_references((m_position==0?nullptr:new typename detail::_aterm_list<Term> const*[m_position]))
     172             :     { 
     173        2595 :       assert(l->function()==detail::g_term_pool().as_list()
     174             :              || l->function()==detail::g_term_pool().as_empty_list());
     175        2595 :       std::size_t j=0;
     176        5197 :       for(detail::_aterm_list<Term> const* t=reinterpret_cast<detail::_aterm_list<Term> const*>(l); 
     177        5197 :               t->function()==detail::g_term_pool().as_list(); 
     178        2602 :               t=reinterpret_cast<detail::_aterm_list<Term> const*>(detail::address(t->tail())), j++)
     179             :       {
     180        2602 :         m_list_element_references[j]=t;
     181             :       }
     182        2595 :     } 
     183             : 
     184             :   public:
     185             :     typedef Term value_type;
     186             :     typedef Term& reference;
     187             :     typedef Term* pointer;
     188             :     typedef ptrdiff_t difference_type;
     189             :     typedef std::forward_iterator_tag iterator_category;
     190             : 
     191             :     /// \brief Default constructor.
     192        5197 :     reverse_term_list_iterator()
     193        5197 :       : m_position(0),
     194        5197 :         m_list_element_references(nullptr)
     195             :     {
     196        5197 :     }
     197             : 
     198             :     /// \brief The copy constructor is not available.
     199             :     /// \param other A sequence of terms
     200             :     reverse_term_list_iterator(const reverse_term_list_iterator& other) = delete;
     201             : 
     202             :     /// \brief Assignment is not available.
     203             :     /// \param other A sequence of terms
     204             :     reverse_term_list_iterator& operator=(const reverse_term_list_iterator& other) = delete;
     205             : 
     206             :     /// \brief Dereference operator on an iterator
     207        5204 :     const Term& operator*() const
     208             :     {
     209        5204 :       assert(m_list_element_references[m_position-1]->function()==detail::g_term_pool().as_list());
     210        5204 :       return m_list_element_references[m_position-1]->head();
     211             :     }
     212             : 
     213             :     /// Arrow operator on an iterator
     214        1083 :     const Term* operator->() const
     215             :     {
     216        1083 :       assert(m_list_element_references[m_position-1]->function()==detail::g_term_pool().as_list());
     217        1083 :       return &(m_list_element_references[m_position-1]->head());
     218             :     }
     219             :     
     220             :     /// \brief Prefix increment operator on iterator.
     221             :     reverse_term_list_iterator& operator++()
     222             :     {
     223             :       assert(m_list_element_references[m_position-1]->function() == detail::g_term_pool().as_list());
     224             :       m_position--;
     225             :       return *this;
     226             :     }
     227             : 
     228             :     /// \brief Postfix increment operator on iterator.
     229        2602 :     void operator++(int)
     230             :     {
     231        2602 :       assert(m_list_element_references[m_position-1]->function() == detail::g_term_pool().as_list());
     232        2602 :       m_position--;
     233        2602 :     }
     234             : 
     235             :     /// \brief Equality of iterators.
     236             :     /// \param other The iterator with which this iterator is compared.
     237             :     /// \return true if the iterators point to the same term_list.
     238        5197 :     bool operator ==(const reverse_term_list_iterator& other) const
     239             :     {
     240        5197 :       return m_position == other.m_position;
     241             :     }
     242             : 
     243             :     /// \brief Inequality of iterators.
     244             :     /// \param other The iterator with which this iterator is compared.
     245             :     /// \return true if the iterators do not point to the same term_list.
     246        5197 :     bool operator !=(const reverse_term_list_iterator& other) const
     247             :     {
     248        5197 :       return !(*this == other);
     249             :     }
     250             : 
     251             :     /// \brief Comparison of iterators.
     252             :     /// \param other The iterator with which this iterator is compared.
     253             :     /// \return true if the pointer to this termlist is smaller than the other pointer.
     254             :     bool operator <(const reverse_term_list_iterator& other) const
     255             :     {
     256             :       return m_position < other.position;
     257             :     }
     258             : 
     259             :     /// \brief Comparison of iterators.
     260             :     /// \param other The iterator with which this iterator is compared.
     261             :     /// \return true if the iterators point to the same term_list.
     262             :     bool operator <=(const reverse_term_list_iterator& other) const
     263             :     {
     264             :       return m_position <= other.m_position;
     265             :     }
     266             : 
     267             :     /// \brief Comparison of iterators.
     268             :     /// \param other The iterator with which this iterator is compared.
     269             :     /// \return true if the iterators point to the same term_list.
     270             :     bool operator >(const reverse_term_list_iterator& other) const
     271             :     {
     272             :       return m_position > other.m_position;
     273             :     }
     274             : 
     275             :     /// \brief Comparison of iterators.
     276             :     /// \param other The iterator with which this iterator is compared.
     277             :     /// \return true if the iterators point to the same term_list.
     278             :     bool operator >=(const reverse_term_list_iterator& other) const
     279             :     {
     280             :       return m_position >= other.m_position;
     281             :     }
     282             : 
     283             : };
     284             : 
     285             : } // namespace atermpp
     286             : 
     287             : #endif // MCRL2_ATERMPP_ATERM_LIST_ITERATOR_H

Generated by: LCOV version 1.14