mirror of https://github.com/OpenTTD/OpenTTD
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.pull/13124/head
parent
bc2513975f
commit
01d1ea6264
|
@ -46,4 +46,19 @@ int find_index(Container const &container, typename Container::const_reference i
|
||||||
return -1;
|
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 */
|
#endif /* CONTAINER_FUNC_HPP */
|
||||||
|
|
Loading…
Reference in New Issue