Changeset e9027b5 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:
f67b4ef
Parents:
871cfe0c
git-author:
Dzejrou <dzejrou@…> (2018-04-23 18:58:08)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:21)
Message:

cpp: fixed some iterator constness issues, added erase to hash_table

File:
1 edited

Legend:

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

    r871cfe0c re9027b5  
    121121            );
    122122        }
     123
     124        template<class Table, class Key>
     125        static typename Table::size_type erase(Table& table, const Key& key)
     126        {
     127            auto idx = table.get_bucket_idx_(key);
     128            auto head = table.table_[idx].head;
     129            auto current = head;
     130
     131            do
     132            {
     133                if (table.key_eq_(key, table.key_extractor_(current->value)))
     134                {
     135                    --table.size_;
     136
     137                    if (current == head)
     138                    {
     139                        if (current->next != head)
     140                            table.table_[idx].head = current->next;
     141                        else
     142                            table.table_[idx].head = nullptr;
     143                    }
     144
     145                    current->unlink();
     146                    delete current;
     147
     148                    return 1;
     149                }
     150                else
     151                    current = current->next;
     152            }
     153            while (current != head);
     154
     155            return 0;
     156        }
    123157    };
    124158
     
    154188            //       link (if key is already in it, place the new copy
    155189            //       next to it, otherwise just return head)
     190        }
     191
     192        template<class Table, class Key>
     193        static typename Table::size_type erase(Table& table, const Key& key)
     194        {
     195            // TODO: erase all items with given key
    156196        }
    157197    };
     
    169209            using iterator_category = forward_iterator_tag;
    170210
    171             hash_table_const_iterator(hash_table_bucket<value_type, size_type>* table = nullptr,
     211            hash_table_const_iterator(const hash_table_bucket<value_type, size_type>* table = nullptr,
    172212                                      size_type idx = size_type{}, size_type max_idx = size_type{},
    173                                       list_node<value_type>* current = nullptr)
     213                                      const list_node<value_type>* current = nullptr)
    174214                : table_{table}, idx_{idx}, max_idx_{max_idx}, current_{current}
    175215            { /* DUMMY BODY */ }
     
    239279            list_node<value_type>* node()
    240280            {
     281                return const_cast<list_node<value_type>*>(current_);
     282            }
     283
     284            const list_node<value_type>* node() const
     285            {
    241286                return current_;
    242287            }
    243288
    244             const list_node<value_type>* node() const
    245             {
    246                 return current_;
     289            size_type idx() const
     290            {
     291                return idx_;
    247292            }
    248293
    249294        private:
    250             hash_table_bucket<value_type, size_type>* table_;
     295            const hash_table_bucket<value_type, size_type>* table_;
    251296            size_type idx_;
    252297            size_type max_idx_;
    253             list_node<value_type>* current_;
     298            const list_node<value_type>* current_;
    254299    };
    255300
     
    358403            }
    359404
     405            size_type idx() const
     406            {
     407                return idx_;
     408            }
     409
    360410            template<class ConstRef, class ConstPtr>
    361411            operator hash_table_const_iterator<
     
    363413            >() const
    364414            {
    365                 return hash_table_const_iterator{
     415                return hash_table_const_iterator<value_type, ConstRef, ConstPtr, size_type>{
    366416                    table_, idx_, max_idx_, current_
    367417                };
     
    401451
    402452            // TODO: requirement for forward iterator is default constructibility, fix others!
    403             hash_table_const_local_iterator(list_node<value_type>* head = nullptr,
    404                                             list_node<value_type>* current = nullptr)
     453            hash_table_const_local_iterator(const list_node<value_type>* head = nullptr,
     454                                            const list_node<value_type>* current = nullptr)
    405455                : head_{head}, current_{current}
    406456            { /* DUMMY BODY */ }
     
    438488            }
    439489
     490
    440491            list_node<value_type>* node()
    441492            {
     493                return const_cast<list_node<value_type>*>(current_);
     494            }
     495
     496            const list_node<value_type>* node() const
     497            {
    442498                return current_;
    443499            }
    444500
    445             const list_node<value_type>* node() const
    446             {
    447                 return current_;
    448             }
    449 
    450501        private:
    451             list_node<value_type>* head_;
    452             list_node<value_type>* current_;
     502            const list_node<value_type>* head_;
     503            const list_node<value_type>* current_;
    453504    };
    454505
     
    530581            >() const
    531582            {
    532                 return hash_table_const_local_iterator{head_, current_};
     583                return hash_table_const_local_iterator<
     584                    value_type, ConstRef, ConstPtr
     585                >{head_, current_};
    533586            }
    534587
     
    659712
    660713                ++size_;
     714                // TODO: if we go over max load factor, rehash
    661715            }
    662716
     
    673727
    674728                ++size_;
     729                // TODO: if we go over max load factor, rehash
    675730            }
    676731
    677732            size_type erase(const key_type& key)
    678733            {
    679                 // TODO: implement
     734                return Policy::erase(*this, key);
    680735            }
    681736
    682737            iterator erase(const_iterator it)
    683738            {
    684                 // TODO: implement
     739                auto node = it.node();
     740                auto idx = it.idx();
     741
     742                /**
     743                 * Note: This way we will continue on the next bucket
     744                 *       if this is the last element in its bucket.
     745                 */
     746                iterator res{table_, idx, size_, node};
     747                ++res;
     748
     749                if (table_[idx].head == node)
     750                {
     751                    if (node->next != node)
     752                        table_[idx].head = node->next;
     753                    else
     754                        table_[idx].head = nullptr;
     755                }
     756
     757                node->unlink();
     758                delete node;
     759
     760                return res;
    685761            }
    686762
Note: See TracChangeset for help on using the changeset viewer.