Package com.jme3.util
Class ListSort<T>
java.lang.Object
com.jme3.util.ListSort<T>
Fast, stable sort used to sort geometries
It's adapted from Tim Peters's work on list sorting for Python. More details
here http://svn.python.org/projects/python/trunk/Objects/listsort.txt
Here is the C code on which this class is based:
http://svn.python.org/projects/python/trunk/Objects/listobject.c
This class was also greatly inspired by the Java7 TimSort by Josh Blosh, with the
difference that temporary memory is allocated as the
geometry list grows and reused throughout the execution of the application.
Usage : ListSort has to be instantiated and kept with the geometry list ( or
w/e it may have to sort) Then the allocate method has to be called to
allocate necessary tmp space needed for the sort. This should be called once
for optimal performance, but can be called several times if the length of the
list changes
Disclaimer : I was intrigued by the use of val >>> 1 in the Java7 Timsort
in place of val / 2 (integer division). Micro benching revealed that val >>> 1
is twice as fast as val / 2 in Java6 and has similar perf in Java7. The
following code uses val >>> 1 whenever a value needs to be divided by 2 and
rounded to its floor.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionfinal void
allocateStack
(int len) Allocate temp variables for the given length This method should be called at least once, but only if the length of the list to sort changed before sortingint
return the useful length of the array being sortedvoid
innerMergeHigh
(Comparator<T> comp, T[] tempArray, T[] arr, int idxA) Attempt to unroll "goto" style original implementation.void
innerMergeLow
(Comparator<T> comp, T[] arr, T[] tempArray) Attempt to unroll "goto" style original implementation.static void
test casevoid
sort
(T[] array, Comparator<T> comparator) Sort the given array given the comparator
-
Constructor Details
-
ListSort
public ListSort()Creates a ListSort
-
-
Method Details
-
allocateStack
public final void allocateStack(int len) Allocate temp variables for the given length This method should be called at least once, but only if the length of the list to sort changed before sorting- Parameters:
len
- the size of the array to sort
-
sort
Sort the given array given the comparator- Parameters:
array
- the array to sortcomparator
- the comparator to compare elements of the array
-
innerMergeLow
Attempt to unroll "goto" style original implementation. this method uses and change temp attributes of the class- Parameters:
comp
- comparatorarr
- the arraytempArray
- the temp array
-
innerMergeHigh
Attempt to unroll "goto" style original implementation. this method uses and change temp attributes of the class- Parameters:
comp
- comparatorarr
- the arraytempArray
- the temp arrayidxA
- the index of the first element of run A
-
getLength
public int getLength()return the useful length of the array being sorted- Returns:
- the length pass to the last allocateStack method
-
main
test case- Parameters:
argv
- ignored
-