Use std::stable_sort instead of mergesort.

* stable_sort is part of the C++ standard, and should work just as well.
This commit is contained in:
Adrien Destugues
2014-05-02 16:36:16 +02:00
parent 6df2ee73b3
commit 77b60d2222
2 changed files with 11 additions and 28 deletions
-11
View File
@@ -69,14 +69,3 @@ snooze_until(bigtime_t time, int timeBase)
{ {
return snooze(time - system_time()); return snooze(time - system_time());
} }
#ifdef __linux__
// Linux is the only system out there to not have this...
int mergesort(void* base, size_t count, size_t size,
int (*compare)(const void *, const void *))
{
qsort(base, count, size, compare);
return 0;
}
#endif
+11 -17
View File
@@ -9,9 +9,9 @@
#include "Gradient.h" #include "Gradient.h"
#include <algorithm>
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <Message.h> #include <Message.h>
@@ -72,17 +72,11 @@ BGradient::ColorStop::operator!=(const ColorStop& other) const
} }
static int static bool
sort_color_stops_by_offset(const void* _left, const void* _right) sort_color_stops_by_offset(const BGradient::ColorStop* left,
const BGradient::ColorStop* right)
{ {
const BGradient::ColorStop** left = (const BGradient::ColorStop**)_left; return left->offset < right->offset;
const BGradient::ColorStop** right = (const BGradient::ColorStop**)_right;
if ((*left)->offset > (*right)->offset)
return 1;
else if ((*left)->offset < (*right)->offset)
return -1;
return 0;
} }
@@ -434,13 +428,13 @@ BGradient::ColorStops() const
void void
BGradient::SortColorStopsByOffset() BGradient::SortColorStopsByOffset()
{ {
// Use merge-sort because it's a stable algorithm: stops with the same // Use stable sort: stops with the same offset will retain their original
// offset will retain their original order. This can be used to have sharp // order. This can be used to have sharp color changes in the gradient.
// color changes in the gradient. // BList.SortItems() uses qsort(), which isn't stable, and sometimes swaps
// BList.SortItems uses a qsort, which isn't stable, and sometimes swaps
// such stops. // such stops.
mergesort(fColorStops.Items(), fColorStops.CountItems(), sizeof(void*), const BGradient::ColorStop** first = (const BGradient::ColorStop**)fColorStops.Items();
sort_color_stops_by_offset); const BGradient::ColorStop** last = first + fColorStops.CountItems();
std::stable_sort(first, last, sort_color_stops_by_offset);
} }