src/generics/migrate.h

Thu, 15 Mar 2018 10:46:46 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 15 Mar 2018 10:46:46 +0200
changeset 1285
0d165d277562
parent 1273
900f1dfae46b
child 1326
69a90bd2dba2
permissions
-rw-r--r--

added signal emissions

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

mercurial