Changeset 2d46556 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:
90adbd7
Parents:
492377a
git-author:
Dzejrou <dzejrou@…> (2018-04-26 15:17:52)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:21)
Message:

cpp: moved logic to the underlying table

File:
1 edited

Legend:

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

    r492377a r2d46556  
    250250            pair<iterator, bool> emplace(Args&&... args)
    251251            {
    252                 /**
    253                  * Note: Some of these modifier functions start off
    254                  *       by incrementing the table's element count and
    255                  *       decrement it when insertion does not take place.
    256                  *       This is because we need to cause rehashing if
    257                  *       there are too many elements, but rehashing itself
    258                  *       would invalidate all pointers we get from
    259                  *       find_insertion_spot, which would require us to
    260                  *       do another find. But this way we avoid two searches
    261                  *       with the possibility of a rehash that is not necessary
    262                  *       (but hardly a bad thing as the table needs just one
    263                  *       element more to rehash).
    264                  *
    265                  *       Also, there are 3 functions with similar bodies,
    266                  *       but the duplicit code cannot be moved to a common
    267                  *       sub-function because all three require different
    268                  *       handling of the value (move, forward and copy).
    269                  */
    270                 table_.increment_size();
    271 
    272                 auto val = value_type{forward<Args>(args)...};
    273                 auto spot = table_.find_insertion_spot(val);
    274                 auto bucket = get<0>(spot);
    275                 auto idx = get<2>(spot);
    276 
    277                 if (!bucket)
    278                     return make_pair(end(), false);
    279 
    280                 auto target = table_.find_node_or_return_head(val, *bucket);
    281                 if (target && table_.keys_equal(val, target->value))
    282                 {
    283                     table_.decrement_size();
    284                     return make_pair(
    285                         iterator{
    286                             table_.table(), idx, table_.bucket_count(),
    287                             target
    288                         },
    289                         false
    290                     );
    291                 }
    292                 else
    293                 {
    294                     auto node = new node_type{move(val)};
    295                     bucket->append(node);
    296 
    297                     return make_pair(iterator{
    298                         table_.table(), idx,
    299                         table_.bucket_count(),
    300                         node
    301                     }, true);
    302                 }
     252                return table_.emplace(forward<Args>(args)...);
    303253            }
    304254
     
    311261            pair<iterator, bool> insert(const value_type& val)
    312262            {
    313                 table_.increment_size();
    314 
    315                 auto spot = table_.find_insertion_spot(val);
    316                 auto bucket = get<0>(spot);
    317                 auto idx = get<2>(spot);
    318 
    319                 if (!bucket)
    320                     return make_pair(end(), false);
    321 
    322                 auto target = table_.find_node_or_return_head(val, *bucket);
    323                 if (target && table_.keys_equal(val, target->value))
    324                 {
    325                     table_.decrement_size();
    326                     return make_pair(
    327                         iterator{
    328                             table_.table(), idx, table_.bucket_count(),
    329                             target
    330                         },
    331                         false
    332                     );
    333                 }
    334                 else
    335                 {
    336                     auto node = new node_type{val};
    337                     bucket->append(node);
    338 
    339                     return make_pair(iterator{
    340                         table_.table(), idx,
    341                         table_.bucket_count(),
    342                         node
    343                     }, true);
    344                 }
     263                return table_.insert(val);
    345264            }
    346265
    347266            pair<iterator, bool> insert(value_type&& val)
    348267            {
    349                 table_.increment_size();
    350 
    351                 auto spot = table_.find_insertion_spot(val);
    352                 auto bucket = get<0>(spot);
    353                 auto idx = get<2>(spot);
    354 
    355                 if (!bucket)
    356                     return make_pair(end(), false);
    357 
    358                 auto target = table_.find_node_or_return_head(val, *bucket);
    359                 if (target && table_.keys_equal(val, target->value))
    360                 {
    361                     table_.decrement_size();
    362                     return make_pair(
    363                         iterator{
    364                             table_.table(), idx, table_.bucket_count(),
    365                             target
    366                         },
    367                         false
    368                     );
    369                 }
    370                 else
    371                 {
    372                     auto node = new node_type{forward<value_type>(val)};
    373                     bucket->append(node);
    374 
    375                     return make_pair(iterator{
    376                         table_.table(), idx,
    377                         table_.bucket_count(),
    378                         node
    379                     }, true);
    380                 }
     268                return table_.insert(forward<value_type>(val));
    381269            }
    382270
Note: See TracChangeset for help on using the changeset viewer.