src/generics/migrate.h

changeset 1273
900f1dfae46b
child 1326
69a90bd2dba2
equal deleted inserted replaced
1272:bf0ac547b934 1273:900f1dfae46b
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013 - 2017 Teemu Piippo
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #pragma once
20 #include <algorithm>
21 #include "range.h"
22
23 template<typename T>
24 void migrate(T& vector, int first, int last, int destination)
25 {
26 if (destination < first)
27 {
28 int n = first - destination;
29 for (int i : range(first, first + 1, last))
30 {
31 for (int step : range(i - 1, i - 2, i - n))
32 std::swap(vector[step + 1], vector[step]);
33 }
34 }
35 else if (destination > last)
36 {
37 int n = destination - last - 1;
38
39 for (int i : range(last, last - 1, first))
40 {
41 for (int step : range(i + 1, i + 2, i + n))
42 std::swap(vector[step - 1], vector[step]);
43 }
44 }
45 }

mercurial