Changeset 404 for trunk


Ignore:
Timestamp:
06/19/15 21:45:40 (10 years ago)
Author:
epyon
Message:
  • pair no longer has single item constructor
  • hash_table no longer depends on pair extension
  • hash_table uses a generic construction mechanism
  • hash_table is no longer copyable, but is movable
  • hash_table can construct non-copyable but movable elements in place
Location:
trunk/nv/stl
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/stl/container/hash_table.hh

    r402 r404  
    171171                }
    172172
     173                hash_table_storage( const hash_table_storage& ) = delete;
     174                hash_table_storage& operator=( const hash_table_storage& ) = delete;
     175
     176                inline hash_table_storage( hash_table_storage&& other )
     177                        : m_buckets( other.m_buckets )
     178                        , m_bucket_count( other.m_bucket_count )
     179                        , m_element_count( other.m_element_count )
     180                        , m_max_load_factor( other.m_max_load_factor )
     181                {
     182                        other.m_buckets = nullptr;
     183                        other.m_bucket_count = 0;
     184                        other.m_element_count = 0;
     185                }
     186                inline hash_table_storage& operator=( hash_table_storage&& other )
     187                {
     188                        if ( this != &other )
     189                        {
     190                                free_nodes( m_buckets, m_bucket_count );
     191                                free_buckets( m_buckets, m_bucket_count );
     192                                m_buckets         = other.m_buckets;
     193                                m_bucket_count    = other.m_bucket_count;
     194                                m_element_count   = other.m_element_count;
     195                                m_max_load_factor = other.max_load_factor;
     196                        }
     197                        return *this;
     198                }
     199
    173200                iterator erase( const_iterator which )
    174201                {
     
    247274
    248275                template < typename... Args >
    249                 iterator insert( size_type index, Args&&... args )
     276                iterator insert( size_type index, hash_type hash_code, Args&&... args )
    250277                {
    251278                        node_type* node = alloc_node();
    252                         base_type::entry_construct( node, nv::forward<Args>( args )... );
     279                        base_type::entry_construct( node, hash_code, nv::forward<Args>( args )... );
    253280                        node->next = m_buckets[index];
    254281                        m_buckets[index] = node;
     
    420447                        const hash_type c = base_type::get_hash( key );
    421448                        size_type       b = base_type::get_bucket_index( c );
    422                         return do_find_node( b, key, c );
     449                        return do_find_node( b, c, key );
    423450                }
    424451                const_iterator find( const key_type& key ) const
     
    426453                        const hash_type c = base_type::get_hash( key );
    427454                        size_type       b = base_type::get_bucket_index( c );
    428                         return do_find_node( b, key, c );
    429                 }
    430 
    431                 inline insert_return_type insert( const value_type& value )
     455                        return do_find_node( b, c, key );
     456                }
     457
     458// TODO: implement
     459//              template< typename... Args >
     460//              insert_return_type emplace( Args&&... args )
     461//              {
     462//
     463//              }
     464
     465                inline insert_return_type insert( value_type&& value )
    432466                {
    433467                        const hash_type h = base_type::get_value_hash( value );
    434468                        size_type       b = base_type::get_bucket_index( h );
    435                         iterator        r = do_find_node( b, value, h );
     469                        iterator        r = do_find_node( b, h, value );
    436470
    437471                        if ( r == this->end() )
    438472                        {
    439                                 if ( base_type::rehash_check( 1 ) )
    440                                 {
    441                                         b = base_type::get_bucket_index( h );
    442                                 }
    443                                 return insert_return_type( base_type::insert( b, value, h ), true );
     473                                if ( base_type::rehash_check( 1 ) ) b = base_type::get_bucket_index( h );
     474                                return insert_return_type( base_type::insert( b, h, nv::forward( value ) ), true );
     475                        }
     476
     477                        return insert_return_type( r, false );
     478                }
     479
     480
     481                inline insert_return_type insert( const value_type& value )
     482                {
     483                        const hash_type h = base_type::get_value_hash( value );
     484                        size_type       b = base_type::get_bucket_index( h );
     485                        iterator        r = do_find_node( b, h, value );
     486
     487                        if ( r == this->end() )
     488                        {
     489                                if ( base_type::rehash_check( 1 ) ) b = base_type::get_bucket_index( h );
     490                                return insert_return_type( base_type::insert( b, h, value ), true );
    444491                        }
    445492
     
    473520                        while ( i != this->cend( b ) )
    474521                        {
    475                                 if ( base_type::entry_compare( i.entry(), key, c ) )
     522                                if ( base_type::entry_compare( i.entry(), c, key ) )
    476523                                {
    477524                                        i = base_type::erase_local( b, i );
     
    490537                        const hash_type h = base_type::get_hash( key );
    491538                        size_type       b = base_type::get_bucket_index( h );
    492                         iterator        r = do_find_node( b, key, h );
     539                        iterator        r = do_find_node( b, h, key );
    493540
    494541                        if ( r == this->end() )
    495542                        {
    496                                 if ( base_type::rehash_check( 1 ) )
    497                                 {
    498                                         b = base_type::get_bucket_index( h );
    499                                 }
    500                                 return insert_return_type( base_type::insert( b, value_type( key ), h ), true );
     543                                if ( base_type::rehash_check( 1 ) ) b = base_type::get_bucket_index( h );
     544                                return insert_return_type( base_type::insert( b, h, key ), true );
    501545                        }
    502546
     
    505549
    506550                template < typename ComparableType >
    507                 iterator do_find_node( size_type index, const ComparableType& query, hash_type h ) const
     551                iterator do_find_node( size_type index, hash_type h, const ComparableType& query ) const
    508552                {
    509553                        const_local_iterator first = this->cbegin( index );
     
    511555                        while ( first != last )
    512556                        {
    513                                 if ( base_type::entry_compare( first.entry(), query, h ) )
     557                                if ( base_type::entry_compare( first.entry(), h, query ) )
    514558                                        return base_type::unlocalize( index, first );
    515559                                ++first;
  • trunk/nv/stl/container/hash_table_policy.hh

    r402 r404  
    193193                };
    194194
    195                 constexpr bool entry_compare( const entry_type* entry, const key_type& key, hash_type ) const
     195                constexpr bool entry_compare( const entry_type* entry, hash_type, const key_type& key ) const
    196196                {
    197197                        return base_type::compare( entry->value.first, key );
    198198                }
    199199
    200                 constexpr bool entry_compare( const entry_type* entry, const value_type& value, hash_type ) const
     200                constexpr bool entry_compare( const entry_type* entry, hash_type, const value_type& value ) const
    201201                {
    202202                        return base_type::compare( entry->value.first, value.first );
    203203                }
    204204
    205                 inline void entry_construct( entry_type* entry, const value_type& value, hash_type ) const
    206                 {
    207                         copy_construct_object( &(entry->value), value );
     205                template < typename... Args >
     206                inline void entry_construct( entry_type* entry, hash_type, Args&&... params ) const
     207                {
     208                        construct_object( &(entry->value), ::nv::forward<Args>( params )... );
     209                }
     210
     211                inline void entry_construct( entry_type* entry, hash_type, Key key ) const
     212                {
     213                        construct_object( &( entry->value ), value_type( ::nv::move( key ), mapped_type() ) );
    208214                }
    209215
     
    239245                };
    240246
    241                 constexpr bool entry_compare( const entry_type* entry, const key_type& key, hash_type h ) const
     247                constexpr bool entry_compare( const entry_type* entry, hash_type h, const key_type& key ) const
    242248                {
    243249                        return entry->hash_code == h && base_type::compare( entry->value.first, key );
    244250                }
    245                 constexpr bool entry_compare( const entry_type* entry, const value_type& value, hash_type h ) const
     251                constexpr bool entry_compare( const entry_type* entry, hash_type h, const value_type& value ) const
    246252                {
    247253                        return entry->hash_code == h &&  base_type::compare( entry->value.first, value.first );
    248254                }
    249255
     256                template < typename... Args >
     257                inline void entry_construct( entry_type* entry, hash_type hash_code, Args&&... params ) const
     258                {
     259                        copy_construct_object( &( entry->value ), ::nv::forward<Args>( params ) );
     260                        entry->hash_code = hash_code;
     261                }
     262
    250263                constexpr hash_type get_entry_hash( const entry_type* entry ) const
    251264                {
     
    256269                        return base_type::get_hash( value.first );
    257270                }
    258                 inline void construct( entry_type* entry, const value_type& value, hash_type hash_code ) const
    259                 {
    260                         copy_construct_object( &(entry->value), value );
    261                         entry->hash_code = hash_code;
    262                 }
    263         };
     271};
    264272       
    265273        struct hash_table_range_mod_policy
  • trunk/nv/stl/memory.hh

    r402 r404  
    124124        inline void raw_construct_object( void* object, Args&&... params )
    125125        {
    126                 new ( object )T( forward<Args>( params )... );
     126                new ( object )T( ::nv::forward<Args>( params )... );
    127127        }
    128128
     
    130130        inline void raw_move_construct_object( void* object, T&& original )
    131131        {
    132                 new ( object )T( move( original ) );
     132                new ( object )T( ::nv::move( original ) );
    133133        }
    134134
     
    142142        inline void construct_object( T* object, Args&&... params )
    143143        {
    144                 new ( object )T( forward<Args>( params )... );
     144                new ( object )T( ::nv::forward<Args>( params )... );
    145145        }
    146146
     
    148148        inline void move_construct_object( T* object, T&& original )
    149149        {
    150                 new ( object )T( move( original ) );
     150                new ( object )T( ::nv::move( original ) );
    151151        }
    152152
  • trunk/nv/stl/utility/pair.hh

    r395 r404  
    2727
    2828                constexpr pair() : first(), second() {}
    29                 constexpr pair( const T1& f, const T2& s )
     29                constexpr pair( const first_type& f, const second_type& s )
    3030                        : first( f ), second( s ) {}
    31                 // TODO: this is non-standard, and I dont like it,
    32                 // but is used in hash_table. Remove?
    33                 constexpr pair( const T1& f )
    34                         : first( f ), second() {}
     31
     32                constexpr pair( first_type&& f, second_type&& s )
     33                        : first( ::nv::forward<first_type>( f ) ), second( ::nv::forward<second_type>( s ) ) {}
    3534
    3635                pair( const pair& ) = default;
     
    3938                this_type& operator= ( this_type&& rhs )
    4039                {
    41                         first = forward<first_type>( rhs.first );
    42                         second = forward<first_type>( rhs.second );
     40                        first = ::nv::forward<first_type>( rhs.first );
     41                        second = ::nv::forward<first_type>( rhs.second );
    4342                }
    4443
Note: See TracChangeset for help on using the changeset viewer.