Changeset f63bef0 in mainline


Ignore:
Timestamp:
2018-07-05T21:41:22Z (6 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
275bdafb
Parents:
6b18e43
git-author:
Dzejrou <dzejrou@…> (2018-04-28 23:45:37)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:22)
Message:

cpp: refactored unnecessary code duplication

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/include/internal/hash_table_iterators.hpp

    r6b18e43 rf63bef0  
    9191            hash_table_const_iterator operator++(int)
    9292            {
    93                 auto tmp_current = current_;
    94                 auto tmp_idx = idx_;
    95 
     93                auto tmp = *this;
     94                ++(*this);
     95
     96                return tmp;
     97            }
     98
     99            list_node<value_type>* node()
     100            {
     101                return const_cast<list_node<value_type>*>(current_);
     102            }
     103
     104            const list_node<value_type>* node() const
     105            {
     106                return current_;
     107            }
     108
     109            size_type idx() const
     110            {
     111                return idx_;
     112            }
     113
     114        private:
     115            const hash_table_bucket<value_type, size_type>* table_;
     116            size_type idx_;
     117            size_type max_idx_;
     118            const list_node<value_type>* current_;
     119    };
     120
     121    template<class Value, class ConstRef, class ConstPtr, class Size>
     122    bool operator==(const hash_table_const_iterator<Value, ConstRef, ConstPtr, Size>& lhs,
     123                    const hash_table_const_iterator<Value, ConstRef, ConstPtr, Size>& rhs)
     124    {
     125        return lhs.node() == rhs.node();
     126    }
     127
     128    template<class Value, class ConstRef, class ConstPtr, class Size>
     129    bool operator!=(const hash_table_const_iterator<Value, ConstRef, ConstPtr, Size>& lhs,
     130                    const hash_table_const_iterator<Value, ConstRef, ConstPtr, Size>& rhs)
     131    {
     132        return !(lhs == rhs);
     133    }
     134
     135    template<class Value, class Reference, class Pointer, class Size>
     136    class hash_table_iterator
     137    {
     138        public:
     139            using value_type      = Value;
     140            using size_type       = Size;
     141            using reference       = Reference;
     142            using pointer         = Pointer;
     143            using difference_type = ptrdiff_t;
     144
     145            using iterator_category = forward_iterator_tag;
     146
     147            hash_table_iterator(hash_table_bucket<value_type, size_type>* table = nullptr,
     148                                size_type idx = size_type{}, size_type max_idx = size_type{},
     149                                list_node<value_type>* current = nullptr)
     150                : table_{table}, idx_{idx}, max_idx_{max_idx}, current_{current}
     151            { /* DUMMY BODY */ }
     152
     153            hash_table_iterator(const hash_table_iterator&) = default;
     154            hash_table_iterator& operator=(const hash_table_iterator&) = default;
     155
     156            reference operator*()
     157            {
     158                return current_->value;
     159            }
     160
     161            pointer operator->()
     162            {
     163                return &current_->value;
     164            }
     165
     166            hash_table_iterator& operator++()
     167            {
    96168                current_ = current_->next;
    97169                if (current_ == table_[idx_].head)
     
    111183                }
    112184
    113                 return hash_table_const_iterator{
    114                     table_, tmp_idx, max_idx_, tmp_current
    115                 };
    116             }
    117 
    118             list_node<value_type>* node()
    119             {
    120                 return const_cast<list_node<value_type>*>(current_);
    121             }
    122 
    123             const list_node<value_type>* node() const
    124             {
    125                 return current_;
    126             }
    127 
    128             size_type idx() const
    129             {
    130                 return idx_;
    131             }
    132 
    133         private:
    134             const hash_table_bucket<value_type, size_type>* table_;
    135             size_type idx_;
    136             size_type max_idx_;
    137             const list_node<value_type>* current_;
    138     };
    139 
    140     template<class Value, class ConstRef, class ConstPtr, class Size>
    141     bool operator==(const hash_table_const_iterator<Value, ConstRef, ConstPtr, Size>& lhs,
    142                     const hash_table_const_iterator<Value, ConstRef, ConstPtr, Size>& rhs)
    143     {
    144         return lhs.node() == rhs.node();
    145     }
    146 
    147     template<class Value, class ConstRef, class ConstPtr, class Size>
    148     bool operator!=(const hash_table_const_iterator<Value, ConstRef, ConstPtr, Size>& lhs,
    149                     const hash_table_const_iterator<Value, ConstRef, ConstPtr, Size>& rhs)
    150     {
    151         return !(lhs == rhs);
    152     }
    153 
    154     template<class Value, class Reference, class Pointer, class Size>
    155     class hash_table_iterator
    156     {
    157         public:
    158             using value_type      = Value;
    159             using size_type       = Size;
    160             using reference       = Reference;
    161             using pointer         = Pointer;
    162             using difference_type = ptrdiff_t;
    163 
    164             using iterator_category = forward_iterator_tag;
    165 
    166             hash_table_iterator(hash_table_bucket<value_type, size_type>* table = nullptr,
    167                                 size_type idx = size_type{}, size_type max_idx = size_type{},
    168                                 list_node<value_type>* current = nullptr)
    169                 : table_{table}, idx_{idx}, max_idx_{max_idx}, current_{current}
    170             { /* DUMMY BODY */ }
    171 
    172             hash_table_iterator(const hash_table_iterator&) = default;
    173             hash_table_iterator& operator=(const hash_table_iterator&) = default;
    174 
    175             reference operator*()
    176             {
    177                 return current_->value;
    178             }
    179 
    180             pointer operator->()
    181             {
    182                 return &current_->value;
    183             }
    184 
    185             hash_table_iterator& operator++()
    186             {
    187                 current_ = current_->next;
    188                 if (current_ == table_[idx_].head)
    189                 {
    190                     if (idx_ < max_idx_)
    191                     {
    192                         while (!table_[++idx_].head && idx_ < max_idx_)
    193                         { /* DUMMY BODY */ }
    194 
    195                         if (idx_ < max_idx_)
    196                             current_ = table_[idx_].head;
    197                         else
    198                             current_ = nullptr;
    199                     }
    200                     else
    201                         current_ = nullptr;
    202                 }
    203 
    204185                return *this;
    205186            }
     
    207188            hash_table_iterator operator++(int)
    208189            {
    209                 auto tmp_current = current_;
    210                 auto tmp_idx = idx_;
    211 
    212                 current_ = current_->next;
    213                 if (current_ == table_[idx_].head)
    214                 {
    215                     if (idx_ < max_idx_)
    216                     {
    217                         while (!table_[++idx_].head && idx_ < max_idx_)
    218                         { /* DUMMY BODY */ }
    219 
    220                         if (idx_ < max_idx_)
    221                             current_ = table_[idx_].head;
    222                         else
    223                             current_ = nullptr;
    224                     }
    225                     else
    226                         current_ = nullptr;
    227                 }
    228 
    229                 return hash_table_iterator{
    230                     table_, tmp_idx, max_idx_, tmp_current
    231                 };
     190                auto tmp = *this;
     191                ++(*this);
     192
     193                return tmp;
    232194            }
    233195
     
    319281            hash_table_const_local_iterator operator++(int)
    320282            {
    321                 auto tmp = current_;
    322                 current_ = current_->next;
    323                 if (current_ == head_)
    324                     current_ = nullptr;
    325 
    326                 return hash_table_const_local_iterator{head_, tmp};
     283                auto tmp = *this;
     284                ++(*this);
     285
     286                return tmp;
    327287            }
    328288
     
    397357            hash_table_local_iterator operator++(int)
    398358            {
    399                 auto tmp = current_;
    400                 current_ = current_->next;
    401                 if (current_ == head_)
    402                     current_ = nullptr;
    403 
    404                 return hash_table_local_iterator{head_, tmp};
     359                auto tmp = *this;
     360                ++(*this);
     361
     362                return tmp;
    405363            }
    406364
Note: See TracChangeset for help on using the changeset viewer.