Changeset b22ccaa 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:
7ea90cf
Parents:
1fafb3e
git-author:
Dzejrou <dzejrou@…> (2018-04-27 23:47:12)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:22)
Message:

cpp: fixed iterators & copy construction, added equality checking for hash tables

File:
1 edited

Legend:

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

    r1fafb3e rb22ccaa  
    577577                    if (idx_ < max_idx_)
    578578                    {
    579                         while (!table_[++idx_].head)
     579                        while (!table_[++idx_].head && idx_ < max_idx_)
    580580                        { /* DUMMY BODY */ }
    581581
     
    602602                    if (idx_ < max_idx_)
    603603                    {
    604                         while (!table_[++idx_].head)
     604                        while (!table_[++idx_].head && idx_ < max_idx_)
    605605                        { /* DUMMY BODY */ }
    606606
     
    693693                    if (idx_ < max_idx_)
    694694                    {
    695                         while (!table_[++idx_].head)
     695                        while (!table_[++idx_].head && idx_ < max_idx_)
    696696                        { /* DUMMY BODY */ }
    697697
     
    718718                    if (idx_ < max_idx_)
    719719                    {
    720                         while (!table_[++idx_].head)
     720                        while (!table_[++idx_].head && idx_ < max_idx_)
    721721                        { /* DUMMY BODY */ }
    722722
     
    996996            {
    997997                for (const auto& x: other)
    998                 {
    999                     auto spot = find_insertion_spot(key_extractor_(x));
    1000                     insert(spot, x);
    1001                 }
     998                    insert(x);
    1002999            }
    10031000
     
    10481045            iterator begin() noexcept
    10491046            {
     1047                auto idx = first_filled_bucket_();
    10501048                return iterator{
    1051                     table_, size_type{}, bucket_count_,
    1052                     table_[0].head
     1049                    table_, idx, bucket_count_,
     1050                    table_[idx].head
    10531051                };
    10541052            }
     
    10711069            const_iterator cbegin() const noexcept
    10721070            {
     1071                auto idx = first_filled_bucket_();
    10731072                return const_iterator{
    1074                     table_, size_type{}, bucket_count_,
    1075                     table_[0].head
     1073                    table_, idx, bucket_count_,
     1074                    table_[idx].head
    10761075                };
    10771076            }
     
    13481347            bool is_eq_to(const hash_table& other) const
    13491348            {
    1350                 // TODO: implement
    1351                 return false;
     1349                if (size() != other.size())
     1350                    return false;
     1351
     1352                auto it = begin();
     1353                while (it != end())
     1354                {
     1355                    /**
     1356                     * For each key K we will check how many
     1357                     * instances of K are there in the table.
     1358                     * Then we will check if the count for K
     1359                     * is equal to that amount.
     1360                     */
     1361
     1362                    size_type cnt{};
     1363                    auto tmp = it;
     1364
     1365                    while (key_eq_(key_extractor_(*it), key_extractor_(*tmp)))
     1366                    {
     1367                        ++cnt;
     1368                        if (++tmp == end())
     1369                            break;
     1370                    }
     1371
     1372                    auto other_cnt = other.count(key_extractor_(*it));
     1373                    if (cnt != other_cnt)
     1374                        return false;
     1375
     1376                    it = tmp; // tmp  is one past *it's key.
     1377                }
     1378
     1379                return true;
    13521380            }
    13531381
     
    14311459            }
    14321460
     1461            size_type first_filled_bucket_() const
     1462            {
     1463                size_type res{};
     1464                while (res < bucket_count_)
     1465                {
     1466                    if (table_[res].head)
     1467                        return res;
     1468                    ++res;
     1469                }
     1470
     1471                /**
     1472                 * Note: This is used for iterators,
     1473                 *       so we need to return a valid index.
     1474                 *       But since table_[0].head is nullptr
     1475                 *       we know that if we return 0 the
     1476                 *       created iterator will test as equal
     1477                 *       to end().
     1478                 */
     1479                return 0;
     1480            }
     1481
    14331482            friend Policy;
    14341483    };
Note: See TracChangeset for help on using the changeset viewer.