From e2324519bffbec5c5c2edd48f597d94a97d4a81a Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Wed, 27 Nov 2024 23:05:58 +0000 Subject: [PATCH] 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. --- src/core/container_func.hpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/core/container_func.hpp b/src/core/container_func.hpp index b0128229c0..6b459cdd08 100644 --- a/src/core/container_func.hpp +++ b/src/core/container_func.hpp @@ -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 +auto Slide(TIter first, TIter last, TIter position) -> std::pair +{ + 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 */