Changeset 8733ce2a in mainline


Ignore:
Timestamp:
2018-07-05T21:41:21Z (6 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
f62f1ee
Parents:
73066e61
git-author:
Dzejrou <dzejrou@…> (2018-04-20 14:47:17)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:21)
Message:

cpp: added list iterators

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/include/impl/list.hpp

    r73066e61 r8733ce2a  
    9090
    9191        template<class T>
    92         class list_const_iterator;
     92        class list_const_iterator
     93        {
     94            public:
     95                using value_type = typename list<T>::value_type;
     96                using reference  = typename list<T>::const_reference;
     97
     98                using iterator_category = forward_iterator_tag;
     99
     100                list_const_iterator(list_node<value_type>* node = nullptr,
     101                                    list_node<value_type>* head = nullptr)
     102                    : current_{node}, head_{head}
     103                { /* DUMMY BODY */ }
     104
     105                list_const_iterator(const list_const_iterator&) = default;
     106                list_const_iterator& operator=(const list_const_iterator&) = default;
     107                list_const_iterator(list_const_iterator&&) = default;
     108                list_const_iterator& operator=(list_const_iterator&&) = default;
     109
     110                reference operator*() const
     111                {
     112                    return current_->value;
     113                }
     114
     115                list_const_iterator& operator++()
     116                {
     117                    if (current_)
     118                    {
     119                        if (current_->next == head_)
     120                            current_ = nullptr;
     121                        else
     122                            current_ = current_->next;
     123                    }
     124
     125                    return *this;
     126                }
     127
     128                list_const_iterator operator++(int)
     129                {
     130                    auto bckp = current_;
     131
     132                    if (current_)
     133                    {
     134                        if (current_->next == head_)
     135                            current_ = nullptr;
     136                        else
     137                            current_ = current_->next;
     138                    }
     139
     140                    return list_const_iterator{bckp};
     141                }
     142
     143                list_node<value_type>* node()
     144                {
     145                    return current_;
     146                }
     147
     148                const list_node<value_type>* node() const
     149                {
     150                    return current_;
     151                }
     152
     153            private:
     154                list_node<value_type>* current_;
     155                list_node<value_type>* head_;
     156        };
     157
     158        template<class T>
     159        bool operator==(const list_const_iterator<T>& lhs, const list_const_iterator<T>& rhs)
     160        {
     161            return lhs.node() == rhs.node();
     162        }
     163
     164        template<class T>
     165        bool operator!=(const list_const_iterator<T>& lhs, const list_const_iterator<T>& rhs)
     166        {
     167            return !(lhs == rhs);
     168        }
    93169
    94170        template<class T>
     
    96172        {
    97173            public:
    98                 using reference = typename list<T>::reference;
    99 
    100                 list_iterator(list_node<T>* node = nullptr)
    101                     : current_{node}
     174                using value_type = typename list<T>::value_type;
     175                using reference  = typename list<T>::reference;
     176
     177                using iterator_category = forward_iterator_tag;
     178
     179                list_iterator(list_node<value_type>* node = nullptr,
     180                              list_node<value_type>* head = nullptr)
     181                    : current_{node}, head_{head}
    102182                { /* DUMMY BODY */ }
    103183
     184                list_iterator(const list_iterator&) = default;
     185                list_iterator& operator=(const list_iterator&) = default;
     186                list_iterator(list_iterator&&) = default;
     187                list_iterator& operator=(list_iterator&&) = default;
     188
    104189                reference operator*()
    105190                {
     
    110195                {
    111196                    if (current_)
    112                         current_ = current_->next;
     197                    {
     198                        if (current_->next == head_)
     199                            current_ = nullptr;
     200                        else
     201                            current_ = current_->next;
     202                    }
    113203
    114204                    return *this;
     
    120210
    121211                    if (current_)
    122                         current_ = current_->next;
     212                    {
     213                        if (current_->next == head_)
     214                            current_ = nullptr;
     215                        else
     216                            current_ = current_->next;
     217                    }
    123218
    124219                    return list_iterator{bckp};
    125220                }
    126221
    127                 list_iterator& operator--()
    128                 {
    129                     if (current_)
    130                         current_ = current_->prev;
    131 
    132                     return *this;
    133                 }
    134 
    135                 list_iterator operator--(int)
    136                 {
    137                     auto bckp = current_;
    138 
    139                     if (current_)
    140                         current_ = current_->prev;
    141 
    142                     return list_iterator{bckp};
    143                 }
    144 
    145                 list_node<T>* node()
     222                list_node<value_type>* node()
    146223                {
    147224                    return current_;
     225                }
     226
     227                const list_node<value_type>* node() const
     228                {
     229                    return current_;
     230                }
     231
     232                operator list_const_iterator<T>() const
     233                {
     234                    return list_const_iterator{current_};
    148235                }
    149236
    150237            private:
    151238                list_node<T>* current_;
     239                list_node<value_type>* head_;
    152240        };
    153241
    154         // TODO: const iterator, iterator must be convertible to const iterator
     242        template<class T>
     243        bool operator==(const list_iterator<T>& lhs, const list_iterator<T>& rhs)
     244        {
     245            return lhs.node() == rhs.node();
     246        }
     247
     248        template<class T>
     249        bool operator!=(const list_iterator<T>& lhs, const list_iterator<T>& rhs)
     250        {
     251            return !(lhs == rhs);
     252        }
    155253    }
    156254
     
    316414            iterator begin() noexcept
    317415            {
    318                 return iterator{head_};
     416                return iterator{head_, head_};
    319417            }
    320418
     
    326424            iterator end() noexcept
    327425            {
    328                 return iterator{};
     426                return iterator{nullptr, head_};
    329427            }
    330428
     
    356454            const_iterator cbegin() const noexcept
    357455            {
    358                 return const_iterator{head_};
     456                return const_iterator{head_, head_};
    359457            }
    360458
    361459            const_iterator cend() const noexcept
    362460            {
    363                 return const_iterator{};
     461                return const_iterator{nullptr, head_};
    364462            }
    365463
     
    510608            }
    511609
     610            template<class... Args>
     611            iterator emplace(const_iterator position, Args&&... args)
     612            {
     613                // TODO: implement
     614            }
     615
    512616        /* private: */
    513617            allocator_type allocator_;
Note: See TracChangeset for help on using the changeset viewer.