Remove BOutlineListView's home-brew quicksort implementation in favor of a comparator that hooks into the STL's sort algorithm as suggested by Ingo. Tested and seems to work nicely.
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25848 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -16,61 +16,23 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
/*!
|
struct ListItemComparator {
|
||||||
\brief A simple recursive quick sort implementations for BListItem arrays.
|
ListItemComparator(int (*compareFunc)(const BListItem *, const BListItem *))
|
||||||
|
: fCompareFunc(compareFunc)
|
||||||
We need to implement it ourselves, since the BOutlineListView sort methods
|
{
|
||||||
use a different comparison function than the standard functions.
|
|
||||||
*/
|
|
||||||
static void
|
|
||||||
quick_sort_item_array(BListItem** items, int32 first, int32 last,
|
|
||||||
int (*compareFunc)(const BListItem* a, const BListItem* b))
|
|
||||||
{
|
|
||||||
if (last <= first)
|
|
||||||
return;
|
|
||||||
|
|
||||||
BListItem* pivot = items[first + (rand() % (last - first))];
|
|
||||||
// choose an arbitrary pivot element
|
|
||||||
|
|
||||||
int32 left = first;
|
|
||||||
int32 right = last;
|
|
||||||
|
|
||||||
do {
|
|
||||||
while ((compareFunc(items[left], pivot) < 0) && (left < last))
|
|
||||||
left++;
|
|
||||||
|
|
||||||
while ((compareFunc(items[right], pivot) > 0) && (right > first))
|
|
||||||
right--;
|
|
||||||
|
|
||||||
if (left < right) {
|
|
||||||
// swap entries
|
|
||||||
BListItem* temp = items[left];
|
|
||||||
items[left] = items[right];
|
|
||||||
items[right] = temp;
|
|
||||||
|
|
||||||
left++;
|
|
||||||
right--;
|
|
||||||
}
|
|
||||||
} while (left < right);
|
|
||||||
|
|
||||||
// if our subset of the array consists of two elements, then we're done recursing
|
|
||||||
if (last - first <= 1)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// At this point, the elements in the left half are all smaller
|
|
||||||
// than the elements in the right half
|
|
||||||
if (first < left) {
|
|
||||||
// sort left half
|
|
||||||
quick_sort_item_array(items, first, left, compareFunc);
|
|
||||||
}
|
}
|
||||||
if (right < last) {
|
|
||||||
// sort right half
|
bool operator()(const BListItem *a, const BListItem *b) const
|
||||||
quick_sort_item_array(items, right, last, compareFunc);
|
{
|
||||||
|
return fCompareFunc(a, b) < 0;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
int (*fCompareFunc)(const BListItem *, const BListItem *);
|
||||||
|
};
|
||||||
|
|
||||||
// #pragma mark -
|
// #pragma mark -
|
||||||
|
|
||||||
|
|
||||||
@@ -662,9 +624,8 @@ void
|
|||||||
BOutlineListView::_SortTree(BList* tree, bool oneLevelOnly,
|
BOutlineListView::_SortTree(BList* tree, bool oneLevelOnly,
|
||||||
int (*compareFunc)(const BListItem* a, const BListItem* b))
|
int (*compareFunc)(const BListItem* a, const BListItem* b))
|
||||||
{
|
{
|
||||||
// home-brewn quick sort for our compareFunc
|
BListItem **items = (BListItem **)tree->Items();
|
||||||
quick_sort_item_array((BListItem**)tree->Items(), 0, tree->CountItems() - 1,
|
std::sort(items, items + tree->CountItems(), ListItemComparator(compareFunc));
|
||||||
compareFunc);
|
|
||||||
|
|
||||||
if (oneLevelOnly)
|
if (oneLevelOnly)
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user