[haiku-commits] haiku: hrev47195 - in src: kits/interface build/libroot

  • From: pulkomandy@xxxxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Fri, 2 May 2014 16:36:28 +0200 (CEST)

hrev47195 adds 1 changeset to branch 'master'
old head: 6df2ee73b35e3a3d1672801a89616cde654e1ac3
new head: 77b60d2222904e326718bf0f1e57efc7cd42ada7
overview: http://cgit.haiku-os.org/haiku/log/?qt=range&q=77b60d2+%5E6df2ee7

----------------------------------------------------------------------------

77b60d2: Use std::stable_sort instead of mergesort.
  
  * stable_sort is part of the C++ standard, and should work just as well.

                             [ Adrien Destugues <pulkomandy@xxxxxxxxxxxxx> ]

----------------------------------------------------------------------------

Revision:    hrev47195
Commit:      77b60d2222904e326718bf0f1e57efc7cd42ada7
URL:         http://cgit.haiku-os.org/haiku/commit/?id=77b60d2
Author:      Adrien Destugues <pulkomandy@xxxxxxxxxxxxx>
Date:        Fri May  2 12:27:26 2014 UTC

----------------------------------------------------------------------------

2 files changed, 11 insertions(+), 28 deletions(-)
src/build/libroot/misc.cpp      | 11 -----------
src/kits/interface/Gradient.cpp | 28 +++++++++++-----------------

----------------------------------------------------------------------------

diff --git a/src/build/libroot/misc.cpp b/src/build/libroot/misc.cpp
index 754e811..52c74f6 100644
--- a/src/build/libroot/misc.cpp
+++ b/src/build/libroot/misc.cpp
@@ -69,14 +69,3 @@ snooze_until(bigtime_t time, int timeBase)
 {
        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
diff --git a/src/kits/interface/Gradient.cpp b/src/kits/interface/Gradient.cpp
index e64c215..218323b 100644
--- a/src/kits/interface/Gradient.cpp
+++ b/src/kits/interface/Gradient.cpp
@@ -9,9 +9,9 @@
 
 #include "Gradient.h"
 
+#include <algorithm>
 #include <math.h>
 #include <stdio.h>
-#include <stdlib.h>
 
 #include <Message.h>
 
@@ -72,17 +72,11 @@ BGradient::ColorStop::operator!=(const ColorStop& other) 
const
 }
 
 
-static int
-sort_color_stops_by_offset(const void* _left, const void* _right)
+static bool
+sort_color_stops_by_offset(const BGradient::ColorStop* left,
+       const BGradient::ColorStop* right)
 {
-       const BGradient::ColorStop** left = (const BGradient::ColorStop**)_left;
-       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;
+       return left->offset < right->offset;
 }
 
 
@@ -434,13 +428,13 @@ BGradient::ColorStops() const
 void
 BGradient::SortColorStopsByOffset()
 {
-       // Use merge-sort because it's a stable algorithm: stops with the same
-       // offset will retain their original order. This can be used to have 
sharp
-       // color changes in the gradient.
-       // BList.SortItems uses a qsort, which isn't stable, and sometimes swaps
+       // Use stable sort: stops with the same offset will retain their 
original
+       // order. This can be used to have sharp color changes in the gradient.
+       // BList.SortItems() uses qsort(), which isn't stable, and sometimes 
swaps
        // such stops.
-       mergesort(fColorStops.Items(), fColorStops.CountItems(), sizeof(void*),
-               sort_color_stops_by_offset);
+       const BGradient::ColorStop** first = (const 
BGradient::ColorStop**)fColorStops.Items();
+       const BGradient::ColorStop** last = first + fColorStops.CountItems();
+       std::stable_sort(first, last, sort_color_stops_by_offset);
 }
 
 


Other related posts: