Skip to content

Commit d9322c1

Browse files
committed
Keyframe::ReorderPoints() use std::sort instead of selection sort
With few points the performance doesn't differ that much; using std::sort removes the possibility of introducing bugs into the handmade sorting algorithm, though.
1 parent 5ba0ecf commit d9322c1

File tree

1 file changed

+6
-16
lines changed

1 file changed

+6
-16
lines changed

src/KeyFrame.cpp

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*/
3030

3131
#include "../include/KeyFrame.h"
32+
#include <algorithm>
3233
#include <utility>
3334

3435
using namespace std;
@@ -38,22 +39,11 @@ using namespace openshot;
3839
// in ascending order based on the point.co.X value. This simplifies
3940
// processing the curve, due to all the points going from left to right.
4041
void Keyframe::ReorderPoints() {
41-
// Loop through all coordinates, and sort them by the X attribute
42-
for (int64_t x = 0; x < Points.size(); x++) {
43-
int64_t compare_index = x;
44-
int64_t smallest_index = x;
45-
46-
for (int64_t compare_index = x + 1; compare_index < Points.size(); compare_index++) {
47-
if (Points[compare_index].co.X < Points[smallest_index].co.X) {
48-
smallest_index = compare_index;
49-
}
50-
}
51-
52-
// swap items
53-
if (smallest_index != compare_index) {
54-
swap(Points[compare_index], Points[smallest_index]);
55-
}
56-
}
42+
std::sort(
43+
begin(Points), end(Points),
44+
[](Point const & l, Point const & r) {
45+
return l.co.X < r.co.X;
46+
});
5747
}
5848

5949
// Constructor which sets the default point & coordinate at X=1

0 commit comments

Comments
 (0)