File indexing completed on 2024-12-22 05:13:36

0001 /*
0002     SPDX-FileCopyrightText: 2015 Ivan Cukic <ivan.cukic(at)kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef UTILS_SLIDE_H
0008 #define UTILS_SLIDE_H
0009 
0010 #include <algorithm>
0011 
0012 // Inspired by C++ Seasoning talk by Sean Parent
0013 
0014 namespace kamd
0015 {
0016 namespace utils
0017 {
0018 template<typename Iterator>
0019 void slide(Iterator f, Iterator l, Iterator p)
0020 {
0021     if (p < f) {
0022         std::rotate(p, f, l);
0023     } else if (l < p) {
0024         std::rotate(f, l, p);
0025     }
0026 }
0027 
0028 template<typename Iterator>
0029 void slide_one(Iterator f, Iterator p)
0030 {
0031     slide(f, f + 1, p);
0032 }
0033 
0034 template<typename Iterator>
0035 void move_one(Iterator from, Iterator to)
0036 {
0037     if (from < to) {
0038         while (from != to) {
0039             using std::swap;
0040             swap(*from, *(from + 1));
0041             ++from;
0042         }
0043     } else {
0044         while (from != to) {
0045             using std::swap;
0046             swap(*from, *(from - 1));
0047             --from;
0048         }
0049     }
0050 }
0051 
0052 } // namespace utils
0053 } // namespace kamd
0054 
0055 #endif // UTILS_SLIDE_H