10 #define DYNASORTIMPL_H 13 #include <type_traits> 22 #define MAKE_SORTLISTTYPE_INSTANCE(C, T) \ 23 template class DynaSortList<C>; \ 24 typedef DynaSortList<C> T##SortList; \ 25 typedef DynaAllocVect<C> T##AllocVect; \ 27 #define MAKE_SORTARRAYTYPE_INSTANCE(C, T) \ 28 template class DynaSortArray<C>; \ 29 typedef DynaSortArray<C> T##SortArray; \ 30 typedef DynaAllocArray<C> T##AllocVect; \ 43 int length = high - low;
44 if ( length < INSERTION_SORT_THRESHOLD ) {
45 for (
int i = low; i < high; ++i )
46 for (
int j = i; j > low && comparator.
compare( dest[ j - 1 ], dest[ j ] ) > 0; --j )
47 _swap( dest, j, j - 1 );
50 int mid = ( low + high ) >> 1;
51 _mergeSort( dest, src, low, mid, comparator );
52 _mergeSort( dest, src, mid, high, comparator );
53 if ( comparator.
compare( src[ mid - 1 ], src[ mid ] ) <= 0 ) {
54 memcpy(dest + low, src + low, length *
sizeof(T*));
57 for(
int d = low, l = low, m = mid; d < high; ++d ) {
58 if ( m >= high || ( l < mid && comparator.
compare( src[ l ], src[ m ] ) <= 0 ) )
59 dest[ d ] = src[ l++ ];
61 dest[ d ] = src[ m++ ];
69 if ( array !=
nullptr && length > 1 && low >= 0 && high < length ) {
71 memcpy(copy, array, length *
sizeof(T*));
72 _mergeSort( copy, array, low, high + 1, comparator );
78 mergeSort( array, length, 0, length - 1, comparator );
82 if ( list !=
nullptr )
87 if ( list !=
nullptr )
102 int length = high - low;
103 if ( length < INSERTION_SORT_THRESHOLD ) {
104 for (
int i = low; i < high; ++i )
105 for (
int j = i; j > low && comparator.
compare( dest[ j - 1 ], dest[ j ] ) > 0; --j )
106 _swap( dest, j, j - 1 );
109 int mid = ( low + high ) >> 1;
110 _mergeSort( dest, src, low, mid, comparator );
111 _mergeSort( dest, src, mid, high, comparator );
112 if ( comparator.
compare( src[ mid - 1 ], src[ mid ] ) <= 0 ) {
113 memcpy(dest + low, src + low, length *
sizeof(T));
116 for(
int d = low, l = low, m = mid; d < high; ++d ) {
117 if ( m >= high || ( l < mid && comparator.
compare( src[ l ], src[ m ] ) <= 0 ) )
118 dest[ d ] = src[ l++ ];
120 dest[ d ] = src[ m++ ];
128 if ( array !=
nullptr && length > 1 && low >= 0 && high < length ) {
130 memcpy(copy, array, length *
sizeof(T));
131 _mergeSort( copy, array, low, high + 1, comparator );
137 mergeSort( array, length, 0, length - 1, comparator );
141 if ( array !=
nullptr )
146 if ( array !=
nullptr )
T * getInternalTypedArray()
Definition: DynaArray.h:72
uint count()
Definition: DynaArray.h:67
static void mergeSort(T **array, int length, int low, int high, IDynaComparator< T *> &comparator)
Sort an array of objects, typically those allocated with "new".
Definition: DynaSortImpl.h:68
static T * newArray(uint count)
Definition: DynaAllocImpl.h:15
Definition: IDynaComparator.h:19
static void _swap(T **x, int a, int b)
Definition: DynaSortImpl.h:35
T ** getInternalTypedArray()
Definition: DynaList.h:86
static void _swap(T *x, int a, int b)
Definition: DynaSortImpl.h:94
Definition: DynaList.h:38
Definition: DynaArray.h:28
virtual int compare(T obj1, T obj2)=0
static void _mergeSort(T **src, T **dest, int low, int high, IDynaComparator< T *> &comparator)
Definition: DynaSortImpl.h:41
Definition: Exception.h:13
static T ** newVect(uint count)
Definition: DynaAllocImpl.h:57
static void mergeSort(T *array, int length, int low, int high, IDynaComparator< T > &comparator)
Sort an array of scalar values.
Definition: DynaSortImpl.h:127
static void _mergeSort(T *src, T *dest, int low, int high, IDynaComparator< T > &comparator)
Definition: DynaSortImpl.h:100
uint count()
Definition: DynaList.h:82