Program Listing for File drag_to.hpp¶
↰ Return to documentation for file (include/sgpl/algorithm/drag_to.hpp)
#pragma once
#ifndef SGPL_ALGORITHM_DRAG_TO_HPP_INCLUDE
#define SGPL_ALGORITHM_DRAG_TO_HPP_INCLUDE
#include <algorithm>
#include <cassert>
#include <iterator>
namespace sgpl {
template<typename RandomIt>
RandomIt drag_to(const RandomIt first, const RandomIt last, const RandomIt to) {
assert( first <= last );
// adapted from https://youtu.be/W2tWOdzgXHA?t=778
// slide backward
if (to < first) {
std::rotate(to, first, last);
return to;
}
// slide forward
else if (last < to) return std::rotate(first, last, to);
else if (first == to) return first;
// rotate (slide target is inside selection)
else return std::rotate(
first,
std::next(first, std::distance(to, last) + 1),
last
);
}
} // namespace sgpl
#endif // #ifndef SGPL_ALGORITHM_DRAG_TO_HPP_INCLUDE