Changeset 108ad4cf 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:
3be3752
Parents:
a4453e1
git-author:
Dzejrou <dzejrou@…> (2018-04-24 23:59:56)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:21)
Message:

cpp: added the rest of the basic insert/emplace functions

File:
1 edited

Legend:

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

    ra4453e1 r108ad4cf  
    3434#include <functional>
    3535#include <memory>
     36#include <type_traits>
    3637#include <utility>
    3738
     
    240241            pair<iterator, bool> emplace(Args&&... args)
    241242            {
    242                 // TODO: implement
     243                // TODO: currently there is code repetition in
     244                //       3 places, try to find a way to reduce it
     245                auto val = value_type{forward<Args>(args)...};
     246                auto key = table_.get_key(val);
     247                auto spot = table_.find_insertion_spot(key);
     248                auto bucket = get<0>(spot);
     249                auto idx = get<2>(spot);
     250
     251                if (bucket->head)
     252                {
     253                    auto head = bucket->head;
     254                    auto current = bucket->head;
     255
     256                    do
     257                    {
     258                        if (table_.keys_equal(key, current->value))
     259                        {
     260                            return make_pair(iterator{
     261                                table_.table(), idx,
     262                                table_.bucket_count(),
     263                                current
     264                            }, false);
     265                        }
     266                        else
     267                            current = current->next;
     268                    }
     269                    while (current != head);
     270                }
     271
     272                auto node = new node_type{move(val)};
     273                bucket->append(node);
     274
     275                return make_pair(iterator{
     276                    table_.table(), idx,
     277                    table_.bucket_count(),
     278                    node
     279                }, true);
    243280            }
    244281
    245282            template<class... Args>
    246             iterator emplace_hint(const_iterator position, Args&&... args)
    247             {
    248                 // TODO: implement
     283            iterator emplace_hint(const_iterator, Args&&... args)
     284            {
     285                return emplace(forward<Args>(args)...).first;
    249286            }
    250287
    251288            pair<iterator, bool> insert(const value_type& val)
    252289            {
    253                 // TODO: implement
     290                auto key = table_.get_key(val);
     291                auto spot = table_.find_insertion_spot(key);
     292                auto bucket = get<0>(spot);
     293                auto idx = get<2>(spot);
     294
     295                if (bucket->head)
     296                {
     297                    auto head = bucket->head;
     298                    auto current = bucket->head;
     299
     300                    do
     301                    {
     302                        if (table_.keys_equal(key, current->value))
     303                        {
     304                            return make_pair(iterator{
     305                                table_.table(), idx,
     306                                table_.bucket_count(),
     307                                current
     308                            }, false);
     309                        }
     310                        else
     311                            current = current->next;
     312                    }
     313                    while (current != head);
     314                }
     315
     316                auto node = new node_type{val};
     317                bucket->append(node);
     318
     319                return make_pair(iterator{
     320                    table_.table(), idx,
     321                    table_.bucket_count(),
     322                    node
     323                }, true);
    254324            }
    255325
    256326            pair<iterator, bool> insert(value_type&& val)
    257327            {
    258                 // TODO: implement
    259             }
    260 
    261             template<class T>
     328                auto key = table_.get_key(val);
     329                auto spot = table_.find_insertion_spot(key);
     330                auto bucket = get<0>(spot);
     331                auto idx = get<2>(spot);
     332
     333                if (bucket->head)
     334                {
     335                    auto head = bucket->head;
     336                    auto current = bucket->head;
     337
     338                    do
     339                    {
     340                        if (table_.keys_equal(key, current->value))
     341                        {
     342                            return make_pair(iterator{
     343                                table_.table(), idx,
     344                                table_.bucket_count(),
     345                                current
     346                            }, false);
     347                        }
     348                        else
     349                            current = current->next;
     350                    }
     351                    while (current != head);
     352                }
     353
     354                auto node = new node_type{forward<value_type>(val)};
     355                bucket->append(node);
     356
     357                return make_pair(iterator{
     358                    table_.table(), idx,
     359                    table_.bucket_count(),
     360                    node
     361                }, true);
     362            }
     363
     364            template<
     365                class T,
     366                class = enable_if_t<is_constructible_v<value_type, T&&>, void>
     367            >
    262368            pair<iterator, bool> insert(T&& val)
    263369            {
    264                 // TODO: implement
    265             }
    266 
    267             iterator insert(const_iterator hint, const value_type& val)
    268             {
    269                 // TODO: implement
    270             }
    271 
    272             iterator insert(const_iterator hint, value_type&& val)
    273             {
    274                 // TODO: implement
    275             }
    276 
    277             template<class T>
     370                return emplace(forward<T>(val));
     371            }
     372
     373            iterator insert(const_iterator, const value_type& val)
     374            {
     375                return insert(val).first;
     376            }
     377
     378            iterator insert(const_iterator, value_type&& val)
     379            {
     380                return insert(forward<value_type>(val)).first;
     381            }
     382
     383            template<
     384                class T,
     385                class = enable_if_t<is_constructible_v<value_type, T&&>, void>
     386            >
    278387            iterator insert(const_iterator hint, T&& val)
    279388            {
    280                 // TODO: implement
     389                return emplace_hint(hint, forward<T>(val));
    281390            }
    282391
     
    284393            void insert(InputIterator first, InputIterator last)
    285394            {
    286                 // TODO: implement
     395                while (first != last)
     396                    insert(*first++);
    287397            }
    288398
     
    357467                return iterator{
    358468                    table_.table(), first.idx(),
    359                     table_.size(), table_.head(first.idx())
     469                    table_.bucket_count(), table_.head(first.idx())
    360470                };
    361471            }
Note: See TracChangeset for help on using the changeset viewer.