- Timestamp:
- 07/22/15 13:19:38 (10 years ago)
- Location:
- trunk/nv/stl
- Files:
-
- 1 deleted
- 5 edited
- 3 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/nv/stl/array.hh
r406 r434 15 15 16 16 #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> 20 20 21 21 namespace nv … … 29 29 30 30 template< typename T > 31 using resizable_dynamic_storage = resizable_storage< dynamic_storage< T > >;31 using resizable_dynamic_storage = sized_storage< dynamic_storage< T > >; 32 32 33 33 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 > >; 35 35 36 36 template< typename T, size_t N > 37 37 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 > >; 41 39 42 40 template< typename T > 43 41 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 > >; 47 43 48 44 } -
trunk/nv/stl/container/fixed_storage.hh
r413 r434 6 6 7 7 /** 8 * @file fixed_ container_handler.hh8 * @file fixed_storage.hh 9 9 * @author Kornel Kisielewicz epyon@chaosforge.org 10 10 * @brief fixed contiguous container handler 11 11 */ 12 12 13 #ifndef NV_STL_CONTAINER_FIXED_ CONTAINER_HANDLER_HH14 #define NV_STL_CONTAINER_FIXED_ CONTAINER_HANDLER_HH13 #ifndef NV_STL_CONTAINER_FIXED_STORAGE_HH 14 #define NV_STL_CONTAINER_FIXED_STORAGE_HH 15 15 16 16 #include <nv/common.hh> … … 22 22 template < 23 23 typename Storage, 24 size_t N, 24 25 typename InitializePolicy = policy_initialize_standard 25 26 > 26 class fixed_ container_handler: public Storage27 class fixed_storage : public Storage 27 28 { 28 29 public: 30 typedef size_t size_type; 29 31 typedef typename Storage::value_type value_type; 30 typedef typename Storage::size_type size_type;31 32 32 fixed_container_handler() 33 static constexpr bool is_fixed = true; 34 35 fixed_storage() 33 36 { 34 InitializePolicy::initialize( Storage::data(), Storage::data() + Storage::capacity() ); 37 Storage::reallocate( N, false ); 38 InitializePolicy::initialize( Storage::data(), Storage::data() + N ); 35 39 } 36 40 37 explicit fixed_ container_handler( default_init )41 explicit fixed_storage( default_init ) 38 42 { 39 uninitialized_construct( Storage::data(), Storage::data() + Storage::capacity() ); 43 Storage::reallocate( 0, false ); 44 uninitialized_construct( Storage::data(), Storage::data() + N ); 40 45 } 41 46 42 explicit fixed_ container_handler( const value_type& v )47 explicit fixed_storage( const value_type& v ) 43 48 { 44 uninitialized_fill( Storage::data(), Storage::data() + Storage::capacity(), v );49 uninitialized_fill( Storage::data(), Storage::data() + N, v ); 45 50 } 46 51 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() 48 62 { 49 InitializePolicy::destroy( Storage::data(), Storage::data() + Storage::capacity());63 InitializePolicy::destroy( Storage::data(), Storage::data() + N ); 50 64 } 51 65 52 66 // 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; 55 69 // 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; 58 72 }; 59 73 60 74 } 61 75 62 #endif // NV_STL_CONTAINER_FIXED_ CONTAINER_HANDLER_HH76 #endif // NV_STL_CONTAINER_FIXED_STORAGE_HH -
trunk/nv/stl/container/growing_storage.hh
r413 r434 6 6 7 7 /** 8 * @file growing_ container_handler.hh8 * @file growing_storage.hh 9 9 * @author Kornel Kisielewicz epyon@chaosforge.org 10 10 * @brief growing contiguous container handler 11 11 */ 12 12 13 #ifndef NV_STL_CONTAINER_GROWING_ CONTAINER_HANDLER_HH14 #define NV_STL_CONTAINER_GROWING_ CONTAINER_HANDLER_HH13 #ifndef NV_STL_CONTAINER_GROWING_STORAGE_HH 14 #define NV_STL_CONTAINER_GROWING_STORAGE_HH 15 15 16 16 #include <nv/common.hh> 17 #include <nv/stl/container/contiguous_storage.hh> 17 18 #include <nv/stl/container/initialize_policy.hh> 18 #include <nv/stl/container/sized_container_handler.hh>19 19 20 20 namespace nv 21 21 { 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 }; 22 34 23 35 template < 24 36 typename Storage, 25 typename InitializePolicy = policy_initialize_standard 37 typename InitializePolicy = policy_initialize_standard, 38 typename NextCapacity = default_next_capacity< size_t > 26 39 > 27 class growing_ container_handler : public sized_container_handler< Storage, InitializePolicy >40 class growing_storage : public Storage 28 41 { 29 42 public: 30 typedef typename growing_container_handler< Storage, InitializePolicy > this_type;31 43 typedef typename Storage::value_type value_type; 32 typedef typename Storage::size_typesize_type;44 typedef size_t size_type; 33 45 typedef value_type* iterator; 34 46 typedef const value_type* const_iterator; 35 47 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 } 37 86 38 87 void reserve( size_type new_capacity ) 39 88 { 40 Storage::try_reserve( new_capacity, true ); 41 } 89 try_reserve( new_capacity, true ); 90 } 91 42 92 void push_back( const value_type& e ) 43 93 { 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 ); 45 95 } 46 96 void push_back( value_type&& e ) 47 97 { 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 ) ); 49 99 } 50 100 template < typename... Args > 51 101 void emplace_back( Args&&... args ) 52 102 { 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 )... ); 54 104 } 55 105 56 106 void pop_back() 57 107 { 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--; 60 111 } 61 112 … … 63 114 iterator erase( iterator position ) 64 115 { 65 iterator iend = Storage::data() + Storage::size(); 116 if ( m_size == 0 ) return; 117 iterator iend = Storage::data() + m_size; 66 118 InitializePolicy::destroy( position ); 67 119 if ( ( position + 1 ) < iend ) 68 120 raw_alias_copy( position + 1, iend, position ); 69 Storage::try_resize( Storage::size() - 1, true );121 m_size--; 70 122 return position; 71 123 } … … 73 125 iterator erase( iterator first, iterator last ) 74 126 { 75 iterator iend = Storage::data() + Storage::size();127 iterator iend = Storage::data() + m_size; 76 128 InitializePolicy::destroy( first, last ); 77 129 iterator position = raw_alias_copy( last, iend, first ); 78 Storage::try_resize( Storage::size() - ( last - first ), true);130 m_size -= ( last - first ); 79 131 return position; 80 132 } … … 85 137 // and use pushback if needed? 86 138 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 ) ) 89 141 InitializePolicy::copy( first, last, Storage::data() + old_size ); 90 142 } … … 93 145 void insert( iterator position, const value_type& value ) 94 146 { 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 ) ) 97 149 { 98 150 raw_alias_copy( position, iend, position + 1 ); … … 106 158 // TODO: distance can't be called on destructive iterators - check 107 159 // and use pushback if needed? 108 iterator iend = Storage::data() + Storage::size();160 iterator iend = Storage::data() + m_size; 109 161 size_type d = distance( first, last ); 110 if ( Storage::try_grow( d ) )162 if ( try_grow( d ) ) 111 163 { 112 164 raw_alias_copy( position, iend, position + d ); … … 114 166 } 115 167 } 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 116 298 }; 117 299 118 300 } 119 301 120 #endif // #define NV_STL_CONTAINER_GROWING_CONTAINER_HANDLER_HH121 302 #endif // NV_STL_CONTAINER_GROWING_STORAGE_HH 303 -
trunk/nv/stl/container/initialize_policy.hh
r404 r434 21 21 22 22 struct default_init 23 { 24 25 }; 26 27 struct struct_policy_initialize_base 23 28 { 24 29 … … 49 54 }; 50 55 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 51 68 struct policy_initialize_never 52 69 { … … 70 87 }; 71 88 72 struct policy_initialize_standard73 {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 102 89 } 103 90 -
trunk/nv/stl/container/sized_storage.hh
r413 r434 6 6 7 7 /** 8 * @file sized_ container_handler.hh8 * @file sized_storage.hh 9 9 * @author Kornel Kisielewicz epyon@chaosforge.org 10 10 * @brief sized contiguous container handler 11 11 */ 12 12 13 #ifndef NV_STL_CONTAINER_SIZED_ CONTAINER_HANDLER_HH14 #define NV_STL_CONTAINER_SIZED_ CONTAINER_HANDLER_HH13 #ifndef NV_STL_CONTAINER_SIZED_STORAGE_HH 14 #define NV_STL_CONTAINER_SIZED_STORAGE_HH 15 15 16 16 #include <nv/common.hh> … … 24 24 typename InitializePolicy = policy_initialize_standard 25 25 > 26 class sized_ container_handler: public Storage26 class sized_storage : public Storage 27 27 { 28 28 public: 29 typedef size_t size_type; 29 30 typedef typename Storage::value_type value_type; 30 typedef typename Storage::size_type size_type;31 31 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 ); } 36 47 37 48 // 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; 40 51 // 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 } 44 66 45 67 inline void resize( size_type new_size ) 46 68 { 47 size_type old_size = Storage::size();69 size_type old_size = m_size; 48 70 resize_impl( new_size ); 49 i nitialize_range( old_size, Storage::size());71 if ( m_size > old_size ) InitializePolicy::initialize( Storage::data() + old_size, Storage::data() + m_size ); 50 72 } 51 73 52 74 inline void resize( size_type new_size, default_init ) 53 75 { 54 size_type old_size = Storage::size();76 size_type old_size = m_size; 55 77 resize_impl( new_size ); 56 i nitialize_range( old_size, Storage::size(), default_init());78 if ( m_size > old_size ) uninitialized_construct( Storage::data() + old_size, Storage::data() + m_size ); 57 79 } 58 80 59 81 inline void resize( size_type new_size, const value_type& value ) 60 82 { 61 size_type old_size = Storage::size();83 size_type old_size = m_size; 62 84 resize_impl( new_size ); 63 i nitialize_range( old_size, Storage::size(), value );85 if ( m_size > old_size ) uninitialized_fill( Storage::data() + old_size, Storage::data() + m_size, value ); 64 86 } 65 87 66 88 inline void assign( const value_type* ptr, size_type sz ) 67 89 { 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 ); 69 91 if ( ptr != nullptr && sz > 0 ) 70 92 { 71 if ( sz != Storage::size() && Storage::try_resize( sz, false ) )93 if ( try_resize( sz, false ) ) 72 94 InitializePolicy::copy( ptr, ptr + sz, Storage::data() ); 73 95 } 74 else Storage::try_resize( 0, false );96 else try_resize( 0, false ); 75 97 } 76 98 … … 81 103 // and use pushback if needed? 82 104 size_type d = distance( first, last ); 83 if ( d != Storage::size() && Storage::try_resize( d, false ) )105 if ( try_resize( d, false ) ) 84 106 InitializePolicy::copy( first, last, Storage::data() ); 85 107 } 86 108 87 109 // explicit copy 88 inline void assign( const sized_ container_handler& other )110 inline void assign( const sized_storage& other ) 89 111 { 90 112 assign( other.data(), other.size() ); … … 93 115 inline void clear() 94 116 { 95 if ( Storage::size()> 0 )117 if ( m_size > 0 ) 96 118 { 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 ); 99 121 } 100 122 } 101 123 102 ~sized_ container_handler()124 ~sized_storage() 103 125 { 104 if ( Storage::size() > 0 )clear();126 clear(); 105 127 } 106 128 107 129 protected: 108 130 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 ) 110 133 { 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; 112 144 } 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 121 146 inline void resize_impl( size_type new_size ) 122 147 { 123 size_type old_size = Storage::size();148 size_type old_size = m_size; 124 149 if ( new_size != old_size ) 125 150 { … … 128 153 InitializePolicy::destroy( Storage::data() + new_size, Storage::data() + old_size ); 129 154 } 130 if ( Storage::try_resize( new_size, true ) )155 if ( try_resize( new_size, true ) ) 131 156 { 132 157 // TODO: error checking … … 134 159 } 135 160 } 136 161 protected: 162 size_type m_size; 137 163 }; 138 164 139 165 } 140 166 141 #endif // NV_STL_CONTAINER_SIZED_ CONTAINER_HANDLER_HH167 #endif // NV_STL_CONTAINER_SIZED_STORAGE_HH -
trunk/nv/stl/memory.hh
r433 r434 19 19 #include <nv/stl/type_traits/properties.hh> 20 20 #include <nv/stl/type_traits/alignment.hh> 21 #include <nv/stl/container/random_access.hh> 21 22 #include <nv/stl/algorithm/fill.hh> 22 23 #include <nv/stl/algorithm/copy.hh> … … 416 417 } 417 418 418 namespace detail419 {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 Super426 {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 Super449 {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 Super480 {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 ) const494 {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 Super502 {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 ) const525 {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 539 419 } 540 420 -
trunk/nv/stl/string/short_string.hh
r433 r434 9 9 10 10 #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 16 namespace 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 } 11 42 12 43 #endif // NV_STL_STRING_SHORT_STRING_HH -
trunk/nv/stl/vector.hh
r395 r434 15 15 16 16 #include <nv/common.hh> 17 #include <nv/stl/container/random_access.hh> 17 18 #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> 20 20 21 21 namespace nv … … 23 23 24 24 template< typename T > 25 using growable_dynamic_storage = grow able_storage< dynamic_storage< T > >;25 using growable_dynamic_storage = growing_storage< dynamic_storage< T > >; 26 26 27 27 template< typename T, size_t N > 28 using growable_static_storage = grow able_storage< static_storage< T, N > >;28 using growable_static_storage = growing_storage< static_storage< T, N > >; 29 29 30 30 template< typename T > 31 31 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 > >; 35 34 36 35 }
Note: See TracChangeset
for help on using the changeset viewer.