Registriert seit: 25. Nov 2005
Ort: München
1.909 Beiträge
Delphi 2010 Professional
|
Re: db4o sortieren
2. Mär 2006, 14:03
Ich habe mal den Rest der Mittagspause geopfert um das hier runterzuhacken.
Ist eigentlich nur ein billiger Quicksort, welcher entweder mit einem IComparer<T> oder einem delegate arbeitet:
Code:
public static class QuickSorter<T>
{
public static void Sort(IList<T> list)
{
Sort(list, Comparer<T>.Default);
}
public static void Sort(IList<T> list, IComparer<T> comparer)
{
Sort(list, comparer, 0, list.Count - 1);
}
public static void Sort(IList<T> list, Comparison<T> comparison)
{
Sort(list, comparison, 0, list.Count - 1);
}
public static void Sort(IList<T> list, int lowerBound, int upperBound)
{
IComparer<T> comparer = Comparer<T>.Default;
Sort(list, comparer, lowerBound, upperBound);
}
public static void Sort(IList<T> list, IComparer<T> comparer, int lowerBound, int upperBound)
{
if (lowerBound < upperBound)
{
int split = Partition(list, comparer, lowerBound, upperBound);
Sort(list, comparer, lowerBound, split - 1);
Sort(list, comparer, split + 1, upperBound);
}
}
public static void Sort(IList<T> list,
Comparison<T> comparison,
int lowerBound,
int upperBound)
{
if (lowerBound < upperBound)
{
int split = Partition(list, comparison, lowerBound, upperBound);
Sort(list, comparison, lowerBound, split - 1);
Sort(list, comparison, split + 1, upperBound);
}
}
static int Partition(IList<T> list,
IComparer<T> comparer,
int lowerBound,
int upperBound)
{
int left = lowerBound + 1;
int right = upperBound;
T pivot = list[lowerBound];
T swap = default(T);
while (left <= right)
{
while (left <= right && comparer.Compare(list[left], pivot) <= 0)
left++;
while (left <= right && comparer.Compare(list[right], pivot) > 0)
right--;
if (left < right)
{
swap = list[left];
list[left] = list[right];
list[right] = swap;
left++;
right--;
}
}
swap = list[lowerBound];
list[lowerBound] = list[right];
list[right] = swap;
return right;
}
static int Partition(IList<T> list,
Comparison<T> comparison,
int lowerBound,
int upperBound)
{
int left = lowerBound + 1;
int right = upperBound;
T pivot = list[lowerBound];
T swap;
while (left <= right)
{
while (left <= right && comparison(list[left], pivot) <= 0)
left++;
while (left <= right && comparison(list[right], pivot) > 0)
right--;
if (left < right)
{
swap = list[left];
list[left] = list[right];
list[right] = swap;
left++;
right--;
}
}
swap = list[lowerBound];
list[lowerBound] = list[right];
list[right] = swap;
return right;
}
}
Er ist wirklich nur schnell auf Vorlage von irgendeinem Quicksort runtergerödelt, also kein Gewehr.
Benutzen kannst du ihn mit jeder IList<T> Implementierung (Die Java deklaration auf deren Seite sieht mir sehr nach einem generischen Container aus ):
Code:
ObjectSet<Person> os = oc.Get(prs);
QuickSorter<Person>.Sort( os, delegate(Person left, Person right)
{ return left.Name.CompareTo(right.Name); });
Robert Giesecke I’m a great believer in “Occam’s Razor,” the principle which says:
“If you say something complicated, I’ll slit your throat.”
|