Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/vector insertion #18

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading