Skip to content

Commit

Permalink
Merge pull request #18 from psiha/fix/vector_insertion
Browse files Browse the repository at this point in the history
Fix/vector insertion
  • Loading branch information
psiha authored Sep 17, 2024
2 parents 53bd40c + 510fc09 commit 64eb030
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions include/psi/vm/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1178,15 +1178,25 @@ class vector

iterator make_space_for_insert( const_iterator const position, size_type const n )
{
using ssize_type = std::make_signed_t<size_type>;

verify_iterator( position );
auto const position_index{ index_of( position ) };
auto const current_size { size() };
auto const new_size { current_size + n };
storage_.expand( to_byte_sz( new_size ) );
auto const elements_to_move_uninitialized_space{ static_cast<size_type>( current_size - position_index ) };
auto const elements_to_move_to_the_current_end { static_cast<size_type>( n - elements_to_move_uninitialized_space ) };
std::uninitialized_move_n( nth( current_size - elements_to_move_uninitialized_space ), elements_to_move_uninitialized_space , nth( static_cast<size_type>( new_size - elements_to_move_uninitialized_space ) ) );
std::move ( nth( position_index ), nth( position_index + elements_to_move_to_the_current_end ), nth( static_cast<size_type>( new_size - n ) ) );
if constexpr ( is_trivially_moveable<T> )
{
std::move( nth( position_index ), nth( position_index + n ), nth( position_index + n ) );
}
else // future support for generic types
{
auto const elements_to_move { static_cast<size_type>( current_size - position_index ) };
auto const elements_to_move_to_uninitialized_space{ n };
auto const elements_to_move_to_the_current_end { static_cast<size_type>( elements_to_move - elements_to_move_to_uninitialized_space ) };
std::uninitialized_move_n( nth( current_size - elements_to_move_to_uninitialized_space ), elements_to_move_to_uninitialized_space, nth( current_size ) );
std::move ( nth( position_index ), nth( position_index + elements_to_move_to_the_current_end ) , nth( position_index + n ) );
}
return nth( position_index );
}
}; // class vector
Expand Down

0 comments on commit 64eb030

Please sign in to comment.