Changeset 434 for trunk


Ignore:
Timestamp:
07/22/15 13:19:38 (10 years ago)
Author:
epyon
Message:
  • simplification of container hierarchy
Location:
trunk/nv/stl
Files:
1 deleted
5 edited
3 moved

Legend:

Unmodified
Added
Removed
  • trunk/nv/stl/array.hh

    r406 r434  
    1515
    1616#include <nv/stl/container/contiguous_storage.hh>
    17 #include <nv/stl/container/contiguous_storage_policy.hh>
    18 #include <nv/stl/container/fixed_container_handler.hh>
    19 #include <nv/stl/container/sized_container_handler.hh>
     17#include <nv/stl/container/fixed_storage.hh>
     18#include <nv/stl/container/sized_storage.hh>
     19#include <nv/stl/container/random_access.hh>
    2020
    2121namespace nv
     
    2929
    3030        template< typename T >
    31         using resizable_dynamic_storage = resizable_storage< dynamic_storage< T > >;
     31        using resizable_dynamic_storage = sized_storage< dynamic_storage< T > >;
    3232
    3333        template< typename T, size_t N >
    34         using resizable_static_storage = resizable_storage< static_storage< T, N > >;
     34        using resizable_static_storage = sized_storage< static_storage< T, N > >;
    3535
    3636        template< typename T, size_t N >
    3737        using array =
    38                 detail::add_random_access <
    39                         detail::add_iterators <
    40                                 fixed_container_handler< fixed_static_storage< T, N > > > >;
     38                random_access < fixed_static_storage< T, N > >;
    4139               
    4240        template< typename T >
    4341        using dynamic_array =
    44                 detail::add_random_access <
    45                         detail::add_iterators <
    46                                 sized_container_handler< resizable_dynamic_storage< T > > > >;
     42                random_access < resizable_dynamic_storage< T > >;
    4743
    4844}
  • trunk/nv/stl/container/fixed_storage.hh

    r413 r434  
    66
    77/**
    8 * @file fixed_container_handler.hh
     8* @file fixed_storage.hh
    99* @author Kornel Kisielewicz epyon@chaosforge.org
    1010* @brief fixed contiguous container handler
    1111*/
    1212
    13 #ifndef NV_STL_CONTAINER_FIXED_CONTAINER_HANDLER_HH
    14 #define NV_STL_CONTAINER_FIXED_CONTAINER_HANDLER_HH
     13#ifndef NV_STL_CONTAINER_FIXED_STORAGE_HH
     14#define NV_STL_CONTAINER_FIXED_STORAGE_HH
    1515
    1616#include <nv/common.hh>
     
    2222        template <
    2323                typename Storage,
     24                size_t N,
    2425                typename InitializePolicy = policy_initialize_standard
    2526        >
    26         class fixed_container_handler : public Storage
     27        class fixed_storage : public Storage
    2728        {
    2829        public:
     30                typedef size_t                       size_type;
    2931                typedef typename Storage::value_type value_type;
    30                 typedef typename Storage::size_type  size_type;
    3132
    32                 fixed_container_handler()
     33                static constexpr bool is_fixed = true;
     34
     35                fixed_storage()
    3336                {
    34                         InitializePolicy::initialize( Storage::data(), Storage::data() + Storage::capacity() );
     37                        Storage::reallocate( N, false );
     38                        InitializePolicy::initialize( Storage::data(), Storage::data() + N );
    3539                }
    3640
    37                 explicit fixed_container_handler( default_init )
     41                explicit fixed_storage( default_init )
    3842                {
    39                         uninitialized_construct( Storage::data(), Storage::data() + Storage::capacity() );
     43                        Storage::reallocate( 0, false );
     44                        uninitialized_construct( Storage::data(), Storage::data() + N );
    4045                }
    4146
    42                 explicit fixed_container_handler( const value_type& v )
     47                explicit fixed_storage( const value_type& v )
    4348                {
    44                         uninitialized_fill( Storage::data(), Storage::data() + Storage::capacity(), v );
     49                        uninitialized_fill( Storage::data(), Storage::data() + N, v );
    4550                }
    4651
    47                 ~fixed_container_handler()
     52                static constexpr size_type max_size() { return N; }
     53                static constexpr size_type capacity() { return N; }
     54                static constexpr size_type size() { return N; }
     55                static constexpr bool empty() { return N == 0; }
     56                static constexpr size_type raw_size() { return sizeof( value_type ) * N; }
     57
     58                operator array_ref< value_type >() { return array_ref< value_type >( Storage::data(), N ); }
     59                operator array_view< value_type >() const { return array_view< value_type >( Storage::data(), N ); }
     60
     61                ~fixed_storage()
    4862                {
    49                         InitializePolicy::destroy( Storage::data(), Storage::data() + Storage::capacity() );
     63                        InitializePolicy::destroy( Storage::data(), Storage::data() + N );
    5064                }
    5165
    5266                // prevent copying
    53                 fixed_container_handler( const fixed_container_handler& ) = delete;
    54                 fixed_container_handler& operator=( const fixed_container_handler& ) = delete;
     67                fixed_storage( const fixed_storage& ) = delete;
     68                fixed_storage& operator=( const fixed_storage& ) = delete;
    5569                // allow move
    56                 fixed_container_handler( fixed_container_handler&& ) = default;
    57                 fixed_container_handler& operator=( fixed_container_handler&& ) = default;
     70                fixed_storage( fixed_storage&& ) = default;
     71                fixed_storage& operator=( fixed_storage&& ) = default;
    5872        };
    5973
    6074}
    6175
    62 #endif // NV_STL_CONTAINER_FIXED_CONTAINER_HANDLER_HH
     76#endif // NV_STL_CONTAINER_FIXED_STORAGE_HH
  • trunk/nv/stl/container/growing_storage.hh

    r413 r434  
    66
    77/**
    8 * @file growing_container_handler.hh
     8* @file growing_storage.hh
    99* @author Kornel Kisielewicz epyon@chaosforge.org
    1010* @brief growing contiguous container handler
    1111*/
    1212
    13 #ifndef NV_STL_CONTAINER_GROWING_CONTAINER_HANDLER_HH
    14 #define NV_STL_CONTAINER_GROWING_CONTAINER_HANDLER_HH
     13#ifndef NV_STL_CONTAINER_GROWING_STORAGE_HH
     14#define NV_STL_CONTAINER_GROWING_STORAGE_HH
    1515
    1616#include <nv/common.hh>
     17#include <nv/stl/container/contiguous_storage.hh>
    1718#include <nv/stl/container/initialize_policy.hh>
    18 #include <nv/stl/container/sized_container_handler.hh>
    1919
    2020namespace nv
    2121{
     22        template< typename SizeType >
     23        struct default_next_capacity
     24        {
     25                static SizeType get( SizeType requested, SizeType capacity, SizeType max_size )
     26                {
     27                        SizeType minimum = nv::max<SizeType>( capacity, 4 );
     28                        SizeType remaining = max_size - capacity;
     29                        if ( remaining < requested ) return 0;
     30                        SizeType additional = nv::max( requested, capacity );
     31                        return nv::max( minimum, remaining < additional ? max_size : capacity + additional );
     32                }
     33        };
    2234
    2335        template <
    2436                typename Storage,
    25                 typename InitializePolicy = policy_initialize_standard
     37                typename InitializePolicy = policy_initialize_standard,
     38                typename NextCapacity = default_next_capacity< size_t >
    2639        >
    27         class growing_container_handler : public sized_container_handler< Storage, InitializePolicy >
     40        class growing_storage : public Storage
    2841        {
    2942        public:
    30                 typedef typename growing_container_handler< Storage, InitializePolicy > this_type;
    3143                typedef typename Storage::value_type value_type;
    32                 typedef typename Storage::size_type  size_type;
     44                typedef size_t                       size_type;
    3345                typedef value_type*                  iterator;
    3446                typedef const value_type*            const_iterator;
    3547
    36                 using sized_container_handler< Storage, InitializePolicy >::sized_container_handler;
     48                static constexpr bool is_fixed = false;
     49
     50                static constexpr size_type max_size() { return size_type( 0x80000000 ); }
     51                constexpr size_t capacity() const { return m_capacity; }
     52                constexpr size_t size() const { return m_size; }
     53                constexpr bool empty() const { return m_size == 0; }
     54                constexpr size_t raw_size() const { return sizeof( value_type ) * m_size; }
     55
     56                operator array_ref< value_type >() { return array_ref< value_type >( Storage::data(), size() ); }
     57                operator array_view< value_type >() const { return array_view< value_type >( Storage::data(), size() ); }
     58
     59                inline growing_storage() : m_size( 0 ), m_capacity( 0 ) {}
     60                inline explicit growing_storage( size_type new_size ) : m_size( 0 ), m_capacity( 0 ) { resize( new_size ); }
     61                inline explicit growing_storage( default_init ) : m_size( 0 ), m_capacity( 0 ) { resize( default_init() ); }
     62                inline growing_storage( size_type new_size, const value_type& v ) : m_size( 0 ), m_capacity( 0 ) { resize( new_size, v ); }
     63
     64                // prevent copying
     65                growing_storage( const growing_storage& ) = delete;
     66                growing_storage& operator=( const growing_storage& ) = delete;
     67               
     68                // allow move
     69                growing_storage( growing_storage&& other )
     70                        : Storage( nv::move( other ) ), m_size( other.m_size ), m_capacity( other.m_capacity )
     71                {
     72                        other.m_size = 0;
     73                        other.m_capacity = 0;
     74                }
     75
     76                inline growing_storage& operator=( growing_storage&& other )
     77                {
     78                        if ( m_capacity > 0 ) Storage::reallocate( 0, false );
     79                        m_size = other.m_size;
     80                        m_capacity = other.m_capacity;
     81                        Storage::operator=( nv::move( other ) );
     82                        other.m_size = 0;
     83                        other.m_capacity = 0;
     84                        return *this;
     85                }
    3786
    3887                void reserve( size_type new_capacity )
    3988                {
    40                         Storage::try_reserve( new_capacity, true );
    41                 }
     89                        try_reserve( new_capacity, true );
     90                }
     91
    4292                void push_back( const value_type& e )
    4393                {
    44                         if ( Storage::try_grow( 1 ) ) copy_construct_object( Storage::data() + Storage::size() - 1, e );
     94                        if ( try_grow( 1 ) ) copy_construct_object( Storage::data() + m_size - 1, e );
    4595                }
    4696                void push_back( value_type&& e )
    4797                {
    48                         if ( Storage::try_grow( 1 ) ) move_construct_object( Storage::data() + Storage::size() - 1, forward<value_type>( e ) );
     98                        if ( try_grow( 1 ) ) move_construct_object( Storage::data() + m_size - 1, forward<value_type>( e ) );
    4999                }
    50100                template < typename... Args >
    51101                void emplace_back( Args&&... args )
    52102                {
    53                         if ( Storage::try_grow( 1 ) ) construct_object( Storage::data() + Storage::size() - 1, forward<Args>( args )... );
     103                        if ( try_grow( 1 ) ) construct_object( Storage::data() + m_size - 1, forward<Args>( args )... );
    54104                }
    55105
    56106                void pop_back()
    57107                {
    58                         InitializePolicy::destroy( Storage::data() + Storage::size() - 1 );
    59                         Storage::try_resize( Storage::size() - 1, true );
     108                        if ( m_size == 0 ) return;
     109                        InitializePolicy::destroy( Storage::data() + m_size - 1 );
     110                        m_size--;
    60111                }
    61112
     
    63114                iterator erase( iterator position )
    64115                {
    65                         iterator iend = Storage::data() + Storage::size();
     116                        if ( m_size == 0 ) return;
     117                        iterator iend = Storage::data() + m_size;
    66118                        InitializePolicy::destroy( position );
    67119                        if ( ( position + 1 ) < iend )
    68120                                raw_alias_copy( position + 1, iend, position );
    69                         Storage::try_resize( Storage::size() - 1, true );
     121                        m_size--;
    70122                        return position;
    71123                }
     
    73125                iterator erase( iterator first, iterator last )
    74126                {
    75                         iterator iend = Storage::data() + Storage::size();
     127                        iterator iend = Storage::data() + m_size;
    76128                        InitializePolicy::destroy( first, last );
    77129                        iterator position = raw_alias_copy( last, iend, first );
    78                         Storage::try_resize( Storage::size() - ( last - first ), true );
     130                        m_size -= ( last - first );
    79131                        return position;
    80132                }
     
    85137                        //   and use pushback if needed?
    86138                        size_type d        = distance( first, last );
    87                         size_type old_size = Storage::size();
    88                         if ( Storage::try_grow( d ) )
     139                        size_type old_size = m_size;
     140                        if ( try_grow( d ) )
    89141                                InitializePolicy::copy( first, last, Storage::data() + old_size );
    90142                }
     
    93145                void insert( iterator position, const value_type& value )
    94146                {
    95                         iterator iend = Storage::data() + Storage::size();
    96                         if ( Storage::try_grow( 1 ) )
     147                        iterator iend = Storage::data() + m_size;
     148                        if ( try_grow( 1 ) )
    97149                        {
    98150                                raw_alias_copy( position, iend, position + 1 );
     
    106158                        // TODO: distance can't be called on destructive iterators - check
    107159                        //   and use pushback if needed?
    108                         iterator iend = Storage::data() + Storage::size();
     160                        iterator iend = Storage::data() + m_size;
    109161                        size_type d = distance( first, last );
    110                         if ( Storage::try_grow( d ) )
     162                        if ( try_grow( d ) )
    111163                        {
    112164                                raw_alias_copy( position, iend, position + d );
     
    114166                        }
    115167                }
     168
     169                inline void resize( size_type new_size )
     170                {
     171                        size_type old_size = m_size;
     172                        resize_impl( new_size );
     173                        if ( m_size > old_size ) InitializePolicy::initialize( Storage::data() + old_size, Storage::data() + m_size );
     174                }
     175
     176                inline void resize( size_type new_size, default_init )
     177                {
     178                        size_type old_size = m_size;
     179                        resize_impl( new_size );
     180                        if ( m_size > old_size ) uninitialized_construct( Storage::data() + old_size, Storage::data() + m_size );
     181                }
     182
     183                inline void resize( size_type new_size, const value_type& value )
     184                {
     185                        size_type old_size = m_size;
     186                        resize_impl( new_size );
     187                        if ( m_size > old_size ) uninitialized_fill( Storage::data() + old_size, Storage::data() + m_size, value );
     188                }
     189
     190                inline void assign( const value_type* ptr, size_type sz )
     191                {
     192                        if ( ptr != nullptr && sz > 0 )
     193                        {
     194                                if ( try_resize( sz, false ) )
     195                                        InitializePolicy::copy( ptr, ptr + sz, Storage::data() );
     196                        }
     197                        else m_size = 0;
     198                }
     199
     200                template< typename InputIterator >
     201                inline void assign( InputIterator first, InputIterator last )
     202                {
     203                        // TODO: distance can't be called on destructive iterators - check
     204                        //   and use pushback if needed?
     205                        if ( m_size > 0 ) InitializePolicy::destroy( Storage::data(), Storage::data() + m_size );
     206                        size_type d = distance( first, last );
     207                        if ( try_resize( d, false ) )
     208                                InitializePolicy::copy( first, last, Storage::data() );
     209                }
     210
     211                // explicit copy
     212                inline void assign( const growing_storage& other )
     213                {
     214                        assign( other.data(), other.size() );
     215                }
     216
     217                inline void clear()
     218                {
     219                        if ( m_size > 0 )
     220                        {
     221                                InitializePolicy::destroy( Storage::data(), Storage::data() + m_size );
     222                                m_size = 0;
     223                        }
     224                }
     225
     226                ~growing_storage()
     227                {
     228                        clear();
     229                        Storage::reallocate( 0, false );
     230                }
     231
     232        protected:
     233
     234                inline void resize_impl( size_type new_size )
     235                {
     236                        size_type old_size = m_size;
     237                        if ( new_size != old_size )
     238                        {
     239                                if ( new_size < old_size )
     240                                {
     241                                        InitializePolicy::destroy( Storage::data() + new_size, Storage::data() + old_size );
     242                                }
     243                                if ( try_resize( new_size, true ) )
     244                                {
     245                                        // TODO: error checking
     246                                }
     247                        }
     248                }
     249
     250                // TODO: return type error checking
     251                bool try_grow( size_t amount )
     252                {
     253                        size_type new_size = amount + m_size;
     254                        if ( new_size > m_capacity )
     255                        {
     256                                size_type new_capacity = NextCapacity::get( new_size - m_capacity, m_capacity, max_size() );
     257                                if ( new_capacity > 0 && Storage::reallocate( new_capacity, true ) )
     258                                {
     259                                        m_capacity = new_capacity;
     260                                }
     261                                else return false;
     262                        }
     263                        m_size = new_size;
     264                        return true;
     265                }
     266                // TODO: return type error checking
     267                bool try_reserve( size_t new_capacity, bool copy_needed )
     268                {
     269                        if ( new_capacity > m_capacity )
     270                        {
     271                                if ( new_capacity > 0 && Storage::reallocate( new_capacity, copy_needed ) )
     272                                {
     273                                        m_capacity = new_capacity;
     274                                }
     275                                else return false;
     276                        }
     277                        return true;
     278                }
     279                // TODO: return type error checking
     280                bool try_resize( size_t new_size, bool copy_needed )
     281                {
     282                        if ( new_size > m_size )
     283                        {
     284                                if ( try_reserve( new_size, copy_needed ) )
     285                                {
     286                                        m_size = new_size;
     287                                        return true;
     288                                }
     289                                return false;
     290                        }
     291                        m_size = new_size;
     292                        return true;
     293                }
     294        protected:
     295                size_type m_size;
     296                size_type m_capacity;
     297
    116298        };
    117299
    118300}
    119301
    120 #endif // #define NV_STL_CONTAINER_GROWING_CONTAINER_HANDLER_HH
    121 
     302#endif // NV_STL_CONTAINER_GROWING_STORAGE_HH
     303
  • trunk/nv/stl/container/initialize_policy.hh

    r404 r434  
    2121
    2222        struct default_init
     23        {
     24
     25        };
     26
     27        struct struct_policy_initialize_base
    2328        {
    2429
     
    4954        };
    5055
     56        struct policy_initialize_standard : policy_initialize_always
     57        {
     58                template < typename ForwardIterator >
     59                inline static void initialize( ForwardIterator first, ForwardIterator last )
     60                {
     61                        typedef typename iterator_traits< ForwardIterator >::value_type value_type;
     62                        if ( !has_trivial_constructor<value_type>() )
     63                                detail::uninitialized_construct_impl( first, last, false_type() );
     64                }
     65        };
     66
     67
    5168        struct policy_initialize_never
    5269        {
     
    7087        };
    7188
    72         struct policy_initialize_standard
    73         {
    74                 template < typename ForwardIterator >
    75                 inline static void initialize( ForwardIterator first, ForwardIterator last )
    76                 {
    77                         typedef typename iterator_traits< ForwardIterator >::value_type value_type;
    78                         if ( !has_trivial_constructor<value_type>() )
    79                                 detail::uninitialized_construct_impl( first, last, false_type() );
    80                 }
    81                 template < typename InputIterator, typename ForwardIterator >
    82                 inline static ForwardIterator copy( InputIterator first, InputIterator last, ForwardIterator out )
    83                 {
    84                         return uninitialized_copy( first, last, out );
    85                 }
    86                 template < typename ForwardIterator >
    87                 inline static void destroy( ForwardIterator first, ForwardIterator last )
    88                 {
    89                         typedef typename iterator_traits< ForwardIterator >::value_type value_type;
    90                         if ( !has_trivial_destructor<value_type>() )
    91                                 detail::uninitialized_destroy_impl( first, last, false_type() );
    92                 }
    93                 template < typename ForwardIterator >
    94                 inline static void destroy( ForwardIterator first )
    95                 {
    96                         typedef typename iterator_traits< ForwardIterator >::value_type value_type;
    97                         if ( !has_trivial_destructor<value_type>() )
    98                                 destroy_object( first );
    99                 }
    100         };
    101 
    10289}
    10390
  • trunk/nv/stl/container/sized_storage.hh

    r413 r434  
    66
    77/**
    8 * @file sized_container_handler.hh
     8* @file sized_storage.hh
    99* @author Kornel Kisielewicz epyon@chaosforge.org
    1010* @brief sized contiguous container handler
    1111*/
    1212
    13 #ifndef NV_STL_CONTAINER_SIZED_CONTAINER_HANDLER_HH
    14 #define NV_STL_CONTAINER_SIZED_CONTAINER_HANDLER_HH
     13#ifndef NV_STL_CONTAINER_SIZED_STORAGE_HH
     14#define NV_STL_CONTAINER_SIZED_STORAGE_HH
    1515
    1616#include <nv/common.hh>
     
    2424                typename InitializePolicy = policy_initialize_standard
    2525        >
    26         class sized_container_handler : public Storage
     26        class sized_storage : public Storage
    2727        {
    2828        public:
     29                typedef size_t                       size_type;
    2930                typedef typename Storage::value_type value_type;
    30                 typedef typename Storage::size_type  size_type;
    3131
    32                 inline sized_container_handler() {}
    33                 inline explicit sized_container_handler( size_type new_size ) { resize( new_size ); }
    34                 inline explicit sized_container_handler( default_init ) { resize( default_init() ); }
    35                 inline sized_container_handler( size_type new_size, const value_type& v ) { resize( new_size, v ); }
     32                static constexpr bool is_fixed = false;
     33
     34                static constexpr size_type max_size() { return size_type( 0x80000000 ); }
     35                constexpr size_t capacity() const { return m_size; }
     36                constexpr size_t size() const { return m_size; }
     37                constexpr bool empty() const { return m_size == 0; }
     38                constexpr size_t raw_size() const { return sizeof( value_type ) * m_size; }
     39
     40                operator array_ref< value_type >() { return array_ref< value_type >( Storage::data(), m_size ); }
     41                operator array_view< value_type >() const { return array_view< value_type >( Storage::data(), m_size ); }
     42
     43                constexpr sized_storage() : m_size( 0 ) {}
     44                inline explicit sized_storage( size_type new_size ) : m_size( 0 ) { resize( new_size ); }
     45                inline explicit sized_storage( default_init ) : m_size( 0 ) { resize( default_init() ); }
     46                inline sized_storage( size_type new_size, const value_type& v ) : m_size( 0 ) { resize( new_size, v ); }
    3647
    3748                // prevent copying
    38                 sized_container_handler( const sized_container_handler& ) = delete;
    39                 sized_container_handler& operator=( const sized_container_handler& ) = delete;
     49                sized_storage( const sized_storage& ) = delete;
     50                sized_storage& operator=( const sized_storage& ) = delete;
    4051                // allow move
    41                 sized_container_handler( sized_container_handler&& ) = default;
    42                 sized_container_handler& operator=( sized_container_handler&& ) = default;
    43 
     52                // allow move
     53                inline sized_storage( sized_storage&& other )
     54                        : Storage( nv::move( other ) ), m_size( other.m_size )
     55                {
     56                        other.m_size = 0;
     57                }
     58                inline sized_storage& operator=( sized_storage&& other )
     59                {
     60                        if ( m_size > 0 ) Storage::reallocate( 0, false );
     61                        m_size = other.m_size;
     62                        Storage::operator=( nv::move( other ) );
     63                        other.m_size = 0;
     64                        return *this;
     65                }
    4466
    4567                inline void resize( size_type new_size )
    4668                {
    47                         size_type old_size = Storage::size();
     69                        size_type old_size = m_size;
    4870                        resize_impl( new_size );
    49                         initialize_range( old_size, Storage::size() );
     71                        if ( m_size > old_size ) InitializePolicy::initialize( Storage::data() + old_size, Storage::data() + m_size );
    5072                }
    5173
    5274                inline void resize( size_type new_size, default_init )
    5375                {
    54                         size_type old_size = Storage::size();
     76                        size_type old_size = m_size;
    5577                        resize_impl( new_size );
    56                         initialize_range( old_size, Storage::size(), default_init() );
     78                        if ( m_size > old_size ) uninitialized_construct( Storage::data() + old_size, Storage::data() + m_size );
    5779                }
    5880
    5981                inline void resize( size_type new_size, const value_type& value )
    6082                {
    61                         size_type old_size = Storage::size();
     83                        size_type old_size = m_size;
    6284                        resize_impl( new_size );
    63                         initialize_range( old_size, Storage::size(), value );
     85                        if ( m_size > old_size ) uninitialized_fill( Storage::data() + old_size, Storage::data() + m_size, value );
    6486                }
    6587
    6688                inline void assign( const value_type* ptr, size_type sz )
    6789                {
    68                         if ( Storage::size() > 0 ) InitializePolicy::destroy( Storage::data(), Storage::data() + Storage::size() );
     90                        if ( m_size > 0 ) InitializePolicy::destroy( Storage::data(), Storage::data() + m_size );
    6991                        if ( ptr != nullptr && sz > 0 )
    7092                        {
    71                                 if ( sz != Storage::size() && Storage::try_resize( sz, false ) )
     93                                if ( try_resize( sz, false ) )
    7294                                        InitializePolicy::copy( ptr, ptr + sz, Storage::data() );
    7395                        }
    74                         else Storage::try_resize( 0, false );
     96                        else try_resize( 0, false );
    7597                }
    7698
     
    81103                        //   and use pushback if needed?
    82104                        size_type d = distance( first, last );
    83                         if ( d != Storage::size() && Storage::try_resize( d, false ) )
     105                        if ( try_resize( d, false ) )
    84106                                InitializePolicy::copy( first, last, Storage::data() );
    85107                }
    86108
    87109                // explicit copy
    88                 inline void assign( const sized_container_handler& other )
     110                inline void assign( const sized_storage& other )
    89111                {
    90112                        assign( other.data(), other.size() );
     
    93115                inline void clear()
    94116                {
    95                         if ( Storage::size() > 0 )
     117                        if ( m_size > 0 )
    96118                        {
    97                                 InitializePolicy::destroy( Storage::data(), Storage::data() + Storage::size() );
    98                                 Storage::try_resize( 0, false );
     119                                InitializePolicy::destroy( Storage::data(), Storage::data() + m_size );
     120                                try_resize( 0, false );
    99121                        }
    100122                }
    101123
    102                 ~sized_container_handler()
     124                ~sized_storage()
    103125                {
    104                         if ( Storage::size() > 0 ) clear();
     126                        clear();
    105127                }
    106128
    107129        protected:
    108130
    109                 inline void initialize_range( size_type old_size, size_type new_size )
     131                // TODO: return type error checking
     132                bool try_resize( size_t new_size, bool copy_needed )
    110133                {
    111                         if ( new_size > old_size ) InitializePolicy::initialize( Storage::data() + old_size, Storage::data() + new_size );
     134                        if ( new_size != m_size )
     135                        {
     136                                if ( Storage::reallocate( new_size, copy_needed ) )
     137                                {
     138                                        m_size = new_size;
     139                                        return true;
     140                                }
     141                                return false;
     142                        }
     143                        return true;
    112144                }
    113                 inline void initialize_range( size_type old_size, size_type new_size, default_init )
    114                 {
    115                         if ( new_size > old_size ) uninitialized_construct( Storage::data() + old_size, Storage::data() + new_size );
    116                 }
    117                 inline void initialize_range( size_type old_size, size_type new_size, const value_type& value )
    118                 {
    119                         if ( new_size > old_size ) uninitialized_fill( Storage::data() + old_size, Storage::data() + new_size, value );
    120                 }
     145
    121146                inline void resize_impl( size_type new_size )
    122147                {
    123                         size_type old_size = Storage::size();
     148                        size_type old_size = m_size;
    124149                        if ( new_size != old_size )
    125150                        {
     
    128153                                        InitializePolicy::destroy( Storage::data() + new_size, Storage::data() + old_size );
    129154                                }
    130                                 if ( Storage::try_resize( new_size, true ) )
     155                                if ( try_resize( new_size, true ) )
    131156                                {
    132157                                        // TODO: error checking
     
    134159                        }
    135160                }
    136 
     161        protected:
     162                size_type m_size;
    137163        };
    138164
    139165}
    140166
    141 #endif // NV_STL_CONTAINER_SIZED_CONTAINER_HANDLER_HH
     167#endif // NV_STL_CONTAINER_SIZED_STORAGE_HH
  • trunk/nv/stl/memory.hh

    r433 r434  
    1919#include <nv/stl/type_traits/properties.hh>
    2020#include <nv/stl/type_traits/alignment.hh>
     21#include <nv/stl/container/random_access.hh>
    2122#include <nv/stl/algorithm/fill.hh>
    2223#include <nv/stl/algorithm/copy.hh>
     
    416417        }
    417418
    418         namespace detail
    419         {
    420 
    421                 template < typename Super, bool Const = Super::is_const >
    422                 class add_iterators {};
    423 
    424                 template < typename Super >
    425                 class add_iterators < Super, true > : public Super
    426                 {
    427                 public:
    428                         typedef typename Super::value_type           value_type;
    429                         typedef const value_type*                    iterator;
    430                         typedef nv::reverse_iterator<iterator>       reverse_iterator;
    431                         typedef const value_type*                    const_iterator;
    432                         typedef nv::reverse_iterator<const_iterator> const_reverse_iterator;
    433 
    434                         using Super::Super;
    435 
    436                         inline const_iterator begin() const { return const_iterator( Super::data() ); }
    437                         inline const_iterator end() const { return const_iterator( Super::data() + Super::size() ); }
    438                         inline const_iterator cbegin() const { return const_iterator( Super::data() ); }
    439                         inline const_iterator cend() const { return const_iterator( Super::data() + Super::size() ); }
    440                         inline const_reverse_iterator rbegin() const { return const_reverse_iterator( end() ); }
    441                         inline const_reverse_iterator crbegin() const { return const_reverse_iterator( end() ); }
    442                         inline const_reverse_iterator rend() const { return const_reverse_iterator( begin() ); }
    443                         inline const_reverse_iterator crend() const { return const_reverse_iterator( begin() ); }
    444                         inline const_iterator iat( size_t i ) const { NV_ASSERT( i <= Super::size(), "Index out of range" ); return begin() + i; }
    445                 };
    446 
    447                 template < typename Super >
    448                 class add_iterators < Super, false > : public Super
    449                 {
    450                 public:
    451                         typedef typename Super::value_type           value_type;
    452                         typedef value_type*                          iterator;
    453                         typedef const value_type*                    const_iterator;
    454                         typedef nv::reverse_iterator<iterator>       reverse_iterator;
    455                         typedef nv::reverse_iterator<const_iterator> const_reverse_iterator;
    456 
    457                         using Super::Super;
    458 
    459                         inline const_iterator begin() const { return const_iterator( Super::data() ); }
    460                         inline const_iterator end() const { return const_iterator( Super::data() + Super::size() ); }
    461                         inline iterator begin() { return iterator( Super::data() ); }
    462                         inline iterator end() { return iterator( Super::data() + Super::size() ); }
    463                         inline const_iterator cbegin() const { return const_iterator( Super::data() ); }
    464                         inline const_iterator cend() const { return const_iterator( Super::data() + Super::size() ); }
    465                         inline reverse_iterator rbegin() { return reverse_iterator( end() ); }
    466                         inline const_reverse_iterator rbegin() const { return const_reverse_iterator( end() ); }
    467                         inline const_reverse_iterator crbegin() const { return const_reverse_iterator( end() ); }
    468                         inline reverse_iterator rend() { return reverse_iterator( begin() ); }
    469                         inline const_reverse_iterator rend() const { return const_reverse_iterator( begin() ); }
    470                         inline const_reverse_iterator crend() const { return const_reverse_iterator( begin() ); }
    471                         inline const_iterator iat( size_t i ) const { NV_ASSERT( i <= Super::size(), "Index out of range" ); return begin() + i; }
    472                         inline iterator       iat( size_t i ) { NV_ASSERT( i <= Super::size(), "Index out of range" ); return begin() + i; }
    473                 };
    474 
    475                 template < typename Super, bool Const = Super::is_const >
    476                 class add_random_access {};
    477 
    478                 template < typename Super >
    479                 class add_random_access < Super, true > : public Super
    480                 {
    481                 public:
    482                         typedef typename Super::value_type           value_type;
    483                         typedef typename Super::size_type            size_type;
    484                         typedef const value_type*                    const_iterator;
    485                         typedef value_type&                          reference;
    486                         typedef const value_type&                    const_reference;
    487 
    488                         using Super::Super;
    489 
    490                         inline const_reference front() const { NV_ASSERT( !Super::empty(), "front() called on empty data!" ); return Super::data()[0]; }
    491                         inline const_reference back() const { NV_ASSERT( !Super::empty(), "front() called on empty data!" ); return Super::data()[Super::size() - 1]; }
    492 
    493                         const_reference operator[]( size_type i ) const
    494                         {
    495                                 NV_ASSERT( i < Super::size(), "Out of range" );
    496                                 return Super::data()[i];
    497                         }
    498                 };
    499 
    500                 template < typename Super >
    501                 class add_random_access < Super, false > : public Super
    502                 {
    503                 public:
    504                         typedef typename Super::value_type           value_type;
    505                         typedef typename Super::size_type            size_type;
    506                         typedef value_type*                          iterator;
    507                         typedef const value_type*                    const_iterator;
    508                         typedef value_type&                          reference;
    509                         typedef const value_type&                    const_reference;
    510 
    511                         using Super::Super;
    512 
    513                         inline reference       front() { NV_ASSERT( !Super::empty(), "front() called on empty data!" );  return Super::data()[0]; }
    514                         inline const_reference front() const { NV_ASSERT( !Super::empty(), "front() called on empty data!" ); return Super::data()[0]; }
    515                         inline reference       back() { NV_ASSERT( !Super::empty(), "front() called on empty data!" ); return Super::data()[Super::size() - 1]; }
    516                         inline const_reference back() const { NV_ASSERT( !Super::empty(), "front() called on empty data!" ); return Super::data()[Super::size() - 1]; }
    517 
    518                         reference operator[]( size_type i )
    519                         {
    520                                 NV_ASSERT( i < Super::size(), "Out of range" );
    521                                 return Super::data()[i];
    522                         }
    523 
    524                         const_reference operator[]( size_type i ) const
    525                         {
    526                                 NV_ASSERT( i < Super::size(), "Out of range" );
    527                                 return Super::data()[i];
    528                         }
    529 
    530                         inline void fill( const value_type& value )
    531                         {
    532                                 fill_n( iterator( Super::data() ), iterator( Super::data() + this->size() ), value );
    533                         }
    534 
    535                 };
    536 
    537         }
    538 
    539419}
    540420
  • trunk/nv/stl/string/short_string.hh

    r433 r434  
    99
    1010#include <nv/stl/string/common.hh>
     11#include <nv/stl/string/string_base.hh>
     12#include <nv/stl/container/random_access.hh>
     13#include <nv/stl/container/contiguous_storage.hh>
     14#include <nv/stl/container/growing_storage.hh>
     15
     16namespace nv
     17{
     18        template < typename Storage >
     19        class string_buffer_ops : Storage
     20        {
     21
     22        };
     23
     24        template < typename T, size_t N >
     25        using short_string =
     26                string_buffer_ops<
     27                        random_access <
     28                                string_base<
     29                                        growing_storage<
     30                                                static_storage< T, N > > > > >;
     31
     32        template < typename T >
     33        using buffer_string =
     34                string_buffer_ops<
     35                        random_access <
     36                                string_base<
     37                                        growing_storage<
     38                                                dynamic_storage< T > > > > >;
     39
     40
     41}
    1142
    1243#endif // NV_STL_STRING_SHORT_STRING_HH
  • trunk/nv/stl/vector.hh

    r395 r434  
    1515
    1616#include <nv/common.hh>
     17#include <nv/stl/container/random_access.hh>
    1718#include <nv/stl/container/contiguous_storage.hh>
    18 #include <nv/stl/container/contiguous_storage_policy.hh>
    19 #include <nv/stl/container/growing_container_handler.hh>
     19#include <nv/stl/container/growing_storage.hh>
    2020
    2121namespace nv
     
    2323
    2424        template< typename T >
    25         using growable_dynamic_storage = growable_storage< dynamic_storage< T > >;
     25        using growable_dynamic_storage = growing_storage< dynamic_storage< T > >;
    2626
    2727        template< typename T, size_t N >
    28         using growable_static_storage = growable_storage< static_storage< T, N > >;
     28        using growable_static_storage = growing_storage< static_storage< T, N > >;
    2929
    3030        template< typename T >
    3131        using vector =
    32                 detail::add_random_access <
    33                         detail::add_iterators <
    34                                 growing_container_handler< growable_dynamic_storage< T > > > >;
     32                random_access <
     33                                growable_dynamic_storage< T > >;
    3534
    3635}
Note: See TracChangeset for help on using the changeset viewer.