Program Listing for File slide_to.hpp

Return to documentation for file (include/sgpl/algorithm/slide_to.hpp)

#pragma once
#ifndef SGPL_ALGORITHM_SLIDE_TO_HPP_INCLUDE
#define SGPL_ALGORITHM_SLIDE_TO_HPP_INCLUDE

#include <algorithm>
#include <cassert>

namespace sgpl {

template<typename RandomIt>
RandomIt slide_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);
  // slide inside selected window
  else return first;

}

} // namespace sgpl

#endif // #ifndef SGPL_ALGORITHM_SLIDE_TO_HPP_INCLUDE