Changeset bb1d15c in mainline


Ignore:
Timestamp:
2018-07-05T21:41:24Z (6 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
c300bb5
Parents:
2e53e83d
git-author:
Dzejrou <dzejrou@…> (2018-05-16 23:14:11)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:24)
Message:

cpp: fixed bugs found by the list tests

File:
1 edited

Legend:

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

    r2e53e83d rbb1d15c  
    3030#define LIBCPP_LIST
    3131
     32#include <__bits/insert_iterator.hpp>
     33#include <__bits/list.hpp>
    3234#include <cstdlib>
    33 #include <__bits/list.hpp>
    3435#include <iterator>
    3536#include <memory>
     
    4344    namespace aux
    4445    {
     46        template<class T>
     47        class list_iterator;
     48
    4549        template<class T>
    4650        class list_const_iterator
     
    5357                using size_type       = typename list<T>::size_type;
    5458
    55                 using iterator_category = forward_iterator_tag;
     59                using iterator_category = bidirectional_iterator_tag;
    5660
    5761                list_const_iterator(list_node<value_type>* node = nullptr,
    58                                     list_node<value_type>* head = nullptr)
    59                     : current_{node}, head_{head}
     62                                    list_node<value_type>* head = nullptr,
     63                                    bool end = true)
     64                    : current_{node}, head_{head}, end_{end}
    6065                { /* DUMMY BODY */ }
    6166
     
    6570                list_const_iterator& operator=(list_const_iterator&&) = default;
    6671
     72                list_const_iterator(const list_iterator<T>& other)
     73                    : current_{other.current_}, head_{other.head_}
     74                { /* DUMMY BODY */ }
     75
    6776                reference operator*() const
    6877                {
     
    7281                list_const_iterator& operator++()
    7382                {
    74                     if (current_)
     83                    if (!end_ && current_)
    7584                    {
    7685                        if (current_->next == head_)
    77                             current_ = nullptr;
     86                            end_ = true;
    7887                        else
    7988                            current_ = current_->next;
     
    8594                list_const_iterator operator++(int)
    8695                {
    87                     auto bckp = current_;
    88 
    89                     if (current_)
     96                    auto old = *this;
     97                    ++(*this);
     98
     99                    return old;
     100                }
     101
     102                list_const_iterator& operator--()
     103                {
     104                    if (end_)
     105                        end_ = false;
     106                    else if (current_)
    90107                    {
    91                         if (current_->next == head_)
    92                             current_ = nullptr;
     108                        if (current_ != head_)
     109                            current_ = current_->prev;
    93110                        else
    94                             current_ = current_->next;
     111                            end_ = true;
    95112                    }
    96113
    97                     return list_const_iterator{bckp};
     114                    return *this;
     115                }
     116
     117                list_const_iterator operator--(int)
     118                {
     119                    auto old = *this;
     120                    --(*this);
     121
     122                    return old;
    98123                }
    99124
     
    123148                }
    124149
     150                bool end() const
     151                {
     152                    return end_;
     153                }
     154
    125155            private:
    126156                list_node<value_type>* current_;
    127157                list_node<value_type>* head_;
     158                bool end_;
    128159        };
    129160
     
    131162        bool operator==(const list_const_iterator<T>& lhs, const list_const_iterator<T>& rhs)
    132163        {
    133             return lhs.node() == rhs.node();
     164            return (lhs.node() == rhs.node()) && (lhs.end() == rhs.end());
    134165        }
    135166
     
    150181                using size_type       = typename list<T>::size_type;
    151182
    152                 using iterator_category = forward_iterator_tag;
     183                using iterator_category = bidirectional_iterator_tag;
    153184
    154185                list_iterator(list_node<value_type>* node = nullptr,
    155                               list_node<value_type>* head = nullptr)
    156                     : current_{node}, head_{head}
     186                              list_node<value_type>* head = nullptr,
     187                              bool end = true)
     188                    : current_{node}, head_{head}, end_{end}
    157189                { /* DUMMY BODY */ }
    158190
     
    162194                list_iterator& operator=(list_iterator&&) = default;
    163195
    164                 reference operator*()
     196                reference operator*() const
    165197                {
    166198                    return current_->value;
     
    169201                list_iterator& operator++()
    170202                {
    171                     if (current_)
     203                    if (!end_ && current_)
    172204                    {
    173205                        if (current_->next == head_)
    174                             current_ = nullptr;
     206                            end_ = true;
    175207                        else
    176208                            current_ = current_->next;
     
    182214                list_iterator operator++(int)
    183215                {
    184                     auto bckp = current_;
    185 
    186                     if (current_)
     216                    auto old = *this;
     217                    ++(*this);
     218
     219                    return old;
     220                }
     221
     222                list_iterator& operator--()
     223                {
     224                    if (end_)
     225                        end_ = false;
     226                    else if (current_)
    187227                    {
    188                         if (current_->next == head_)
    189                             current_ = nullptr;
     228                        if (current_ != head_)
     229                            current_ = current_->prev;
    190230                        else
    191                             current_ = current_->next;
     231                            end_ = true;
    192232                    }
    193233
    194                     return list_iterator{bckp};
     234                    return *this;
     235                }
     236
     237                list_iterator operator--(int)
     238                {
     239                    auto old = *this;
     240                    --(*this);
     241
     242                    return old;
    195243                }
    196244
     
    225273                }
    226274
     275                bool end() const
     276                {
     277                    return end_;
     278                }
     279
    227280            private:
    228281                list_node<value_type>* current_;
    229282                list_node<value_type>* head_;
     283                bool end_;
    230284        };
    231285
     
    233287        bool operator==(const list_iterator<T>& lhs, const list_iterator<T>& rhs)
    234288        {
    235             return lhs.node() == rhs.node();
     289            return (lhs.node() == rhs.node()) && (lhs.end() == rhs.end());
    236290        }
    237291
     
    283337            {
    284338                init_(
    285                     aux::insert_iterator<value_type>{value_type{}},
    286                     aux::insert_iterator<value_type>{size_}
     339                    aux::insert_iterator<value_type>{size_type{}, value_type{}},
     340                    aux::insert_iterator<value_type>{size_, value_type{}}
    287341                );
    288342            }
     
    293347            {
    294348                init_(
    295                     aux::insert_iterator<value_type>{val},
    296                     aux::insert_iterator<value_type>{n}
     349                    aux::insert_iterator<value_type>{size_type{}, val},
     350                    aux::insert_iterator<value_type>{n, value_type{}}
    297351                );
    298352            }
     
    316370            {
    317371                other.head_ = nullptr;
     372                other.size_ = size_type{};
    318373            }
    319374
    320375            list(const list& other, const allocator_type alloc)
    321                 : allocator_{alloc}, head_{nullptr}, size_{other.size_}
    322             {
     376                : allocator_{alloc}, head_{nullptr}, size_{}
     377            { // Size is set in init_.
    323378                init_(other.begin(), other.end());
    324379            }
     
    330385            {
    331386                other.head_ = nullptr;
     387                other.size_ = size_type{};
    332388            }
    333389
     
    350406
    351407                init_(other.begin(), other.end());
     408
     409                return *this;
    352410            }
    353411
     
    357415                fini_();
    358416
     417                head_ = move(other.head_);
     418                size_ = move(other.size_);
    359419                allocator_ = move(other.allocator_);
    360420
    361                 init_(
    362                     make_move_iterator(other.begin()),
    363                     make_move_iterator(other.end())
    364                 );
     421                other.head_ = nullptr;
     422                other.size_ = size_type{};
     423
     424                return *this;
    365425            }
    366426
     
    370430
    371431                init_(init.begin(), init.end());
     432
     433                return *this;
    372434            }
    373435
     
    385447
    386448                init_(
    387                     aux::insert_iterator<value_type>{val},
    388                     aux::insert_iterator<value_type>{n}
     449                    aux::insert_iterator<value_type>{size_type{}, val},
     450                    aux::insert_iterator<value_type>{n, value_type{}}
    389451                );
    390452            }
     
    404466            iterator begin() noexcept
    405467            {
    406                 return iterator{head_, head_};
     468                return iterator{head_, head_, size_ == 0U};
    407469            }
    408470
     
    414476            iterator end() noexcept
    415477            {
    416                 return iterator{nullptr, head_};
     478                return iterator{get_last_(), head_, true};
    417479            }
    418480
     
    444506            const_iterator cbegin() const noexcept
    445507            {
    446                 return const_iterator{head_, head_};
     508                return const_iterator{head_, head_, size_ == 0U};
    447509            }
    448510
    449511            const_iterator cend() const noexcept
    450512            {
    451                 return const_iterator{nullptr, head_};
     513                return const_iterator{get_last_(), head_, true};
    452514            }
    453515
     
    608670                    head_ = head_->prev;
    609671
    610                 return iterator{node->prev, head_};
     672                return iterator{node->prev, head_, false};
    611673            }
    612674
     
    625687                return insert(
    626688                    position,
    627                     aux::insert_iterator<value_type>{0u, val},
    628                     aux::insert_iterator<value_type>{n}
     689                    aux::insert_iterator<value_type>{size_type{}, val},
     690                    aux::insert_iterator<value_type>{n, value_type{}}
    629691                );
    630692            }
     
    642704                }
    643705
    644                 return iterator{position.node()->next, head_};
     706                return iterator{position.node()->next, head_, false};
    645707            }
    646708
     
    675737                delete node;
    676738
    677                 return iterator{next, head_};
     739                return iterator{next, head_, size_ == 0U};
    678740            }
    679741
     
    703765                }
    704766
    705                 return iterator{next, head_};
     767                return iterator{next, head_, size_ == 0U};
    706768            }
    707769
     
    10331095            }
    10341096
    1035             aux::list_node<value_type>* get_last_()
     1097            aux::list_node<value_type>* get_last_() const
    10361098            {
    10371099                if (!head_)
Note: See TracChangeset for help on using the changeset viewer.