Changeset 7f379fe 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:
cfeeb61
Parents:
54618da
git-author:
Dzejrou <dzejrou@…> (2018-04-25 20:39:07)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:21)
Message:

cpp: implemented multi policy operations, fixed constness of some parameters

File:
1 edited

Legend:

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

    r54618da r7f379fe  
    192192            typename Table::iterator,
    193193            typename Table::iterator
    194         > equal_range(Table& table, const Key& key)
     194        > equal_range(const Table& table, const Key& key)
    195195        {
    196196            auto it = table.find(key);
     
    202202            typename Table::const_iterator,
    203203            typename Table::const_iterator
    204         > equal_range_const(Table& table, const Key& key)
     204        > equal_range_const(const Table& table, const Key& key)
    205205        { // Note: We cannot overload by return type, so we use a different name.
    206206            auto it = table.find(key);
     
    235235        static typename Table::hint_type find_insertion_spot(const Table& table, const Key& key)
    236236        {
    237             // TODO: find the right bucket, in that, find the right
    238             //       link (if key is already in it, place the new copy
    239             //       next to it, otherwise just return head)
     237            auto idx = table.get_bucket_idx_(key);
     238            auto head = table.table_[idx].head;
     239
     240            if (head)
     241            {
     242                auto current = head;
     243                do
     244                {
     245                    if (table.keys_equal(key, table.get_key(current->value)))
     246                    {
     247                        return make_tuple(
     248                            &table.table_[idx],
     249                            current,
     250                            idx
     251                        );
     252                    }
     253
     254                    current = current->next;
     255                } while (current != head);
     256            }
     257
     258            return make_tuple(
     259                &table.table_[idx],
     260                table.table_[idx].head,
     261                idx
     262            );
    240263        }
    241264
     
    243266        static typename Table::size_type erase(Table& table, const Key& key)
    244267        {
    245             // TODO: erase all items with given key
     268            auto idx = table.get_bucket_idx_(key);
     269            auto it = table.begin(it);
     270            typename Table::size_type res{};
     271
     272            while (it != table.end(it))
     273            {
     274                if (table.keys_equal(key, table.get_key(*it)))
     275                {
     276                    while (table.keys_equal(key, table.get_key(*it)))
     277                    {
     278                        auto node = it.node();
     279                        ++it;
     280                        ++res;
     281
     282                        node.unlink();
     283                        delete node;
     284                        --table.size_;
     285                    }
     286
     287                    // Elements with equal keys are next to each other.
     288                    return res;
     289                }
     290
     291                ++it;
     292            }
     293
     294            return res;
    246295        }
    247296
     
    250299            typename Table::iterator,
    251300            typename Table::iterator
    252         > equal_range(Table& table, const Key& key)
    253         {
    254             // TODO: implement
     301        > equal_range(const Table& table, const Key& key)
     302        {
     303            auto first = table.find(key);
     304            if (first == table.end())
     305                return make_pair(end(), end());
     306
     307            auto last = first;
     308            while (table.keys_equal(key, table.get_key(*last)))
     309                ++last;
     310
     311            // The second iterator points one behind the range.
     312            ++last;
     313
     314            return make_pair(first, last);
    255315        }
    256316
     
    259319            typename Table::const_iterator,
    260320            typename Table::const_iterator
    261         > equal_range_const(Table& table, const Key& key)
    262         {
    263             // TODO: implement
     321        > equal_range_const(const Table& table, const Key& key)
     322        {
     323            auto first = table.find(key);
     324            if (first == table.end())
     325                return make_pair(end(), end());
     326
     327            auto last = first;
     328            while (table.keys_equal(key, table.get_key(*last)))
     329                ++last;
     330
     331            // The second iterator points one behind the range.
     332            ++last;
     333
     334            return make_pair(first, last);
    264335        }
    265336    };
Note: See TracChangeset for help on using the changeset viewer.