Codechange: Add Slide container helper function.

This function will move the selected range between first and last to position, rotating elements as necessary.

Returns iterators to the new positions.
This commit is contained in:
Peter Nelson 2024-11-27 23:05:58 +00:00 committed by Peter Nelson
parent bc2513975f
commit 01d1ea6264

View File

@ -46,4 +46,19 @@ int find_index(Container const &container, typename Container::const_reference i
return -1;
}
/**
* Move elements between first and last to a new position, rotating elements in between as necessary.
* @param first Iterator to first element to move.
* @param last Iterator to (end-of) last element to move.
* @param position Iterator to where range should be moved to.
* @returns Iterators to first and last after being moved.
*/
template <typename TIter>
auto Slide(TIter first, TIter last, TIter position) -> std::pair<TIter, TIter>
{
if (last < position) return { std::rotate(first, last, position), position };
if (position < first) return { position, std::rotate(position, first, last) };
return { first, last };
}
#endif /* CONTAINER_FUNC_HPP */