Dynalib Utils
DynaSortImpl.h
Go to the documentation of this file.
1 
9 #ifndef DYNASORTIMPL_H
10 #define DYNASORTIMPL_H
11 
12 #include <iostream>
13 #include <type_traits>
14 
15 #include "DynaSort.h"
16 #include "BitManip.h"
17 #include "DynaAllocImpl.h"
18 #include "DynaList.h"
19 #include "CheckForError.h"
20 #include "Exception.h"
21 
22 #define MAKE_SORTLISTTYPE_INSTANCE(C, T) \
23  template class DynaSortList<C>; \
24  typedef DynaSortList<C> T##SortList; \
25  typedef DynaAllocVect<C> T##AllocVect; \
26 
27 #define MAKE_SORTARRAYTYPE_INSTANCE(C, T) \
28  template class DynaSortArray<C>; \
29  typedef DynaSortArray<C> T##SortArray; \
30  typedef DynaAllocArray<C> T##AllocVect; \
31 
32 //===========================================================================
33 // DynaSortList
34 //===========================================================================
35 template <class T> void DynaSortList<T>::_swap( T** x, int a, int b ) {
36  T* t = x[ a ];
37  x[ a ] = x[ b ];
38  x[ b ] = t;
39 }
40 
41 template <class T> void DynaSortList<T>::_mergeSort( T** src, T** dest, int low, int high, IDynaComparator<T*>& comparator ) {
42  try {
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 );
48  return;
49  }
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*));
55  return;
56  }
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++ ];
60  else
61  dest[ d ] = src[ m++ ];
62  }
63  }
64  catch ( Exception e ) {
65  }
66 }
67 
68 template <class T> void DynaSortList<T>::mergeSort( T** array, int length, int low, int high, IDynaComparator<T*>& comparator ) {
69  if ( array != nullptr && length > 1 && low >= 0 && high < length ) {
70  auto copy = DynaAllocVect<T>::newVect(length);
71  memcpy(copy, array, length * sizeof(T*));
72  _mergeSort( copy, array, low, high + 1, comparator );
73  delete copy;
74  }
75 }
76 
77 template <class T> void DynaSortList<T>::mergeSort( T** array, int length, IDynaComparator<T*>& comparator ) {
78  mergeSort( array, length, 0, length - 1, comparator );
79 }
80 
81 template <class T> void DynaSortList<T>::mergeSort( DynaList<T>* list, int low, int high, IDynaComparator<T*>& comparator ) {
82  if ( list != nullptr )
83  mergeSort( list->getInternalTypedArray(), list->count(), low, high, comparator );
84 }
85 
86 template <class T> void DynaSortList<T>::mergeSort( DynaList<T>* list, IDynaComparator<T*>& comparator ) {
87  if ( list != nullptr )
88  mergeSort( list->getInternalTypedArray(), list->count(), 0, list->count() - 1, comparator );
89 }
90 
91 //===========================================================================
92 // DynaSortArray
93 //===========================================================================
94 template <class T> void DynaSortArray<T>::_swap( T* x, int a, int b ) {
95  T t = x[ a ];
96  x[ a ] = x[ b ];
97  x[ b ] = t;
98 }
99 
100 template <class T> void DynaSortArray<T>::_mergeSort( T* src, T* dest, int low, int high, IDynaComparator<T>& comparator ) {
101  try {
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 );
107  return;
108  }
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));
114  return;
115  }
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++ ];
119  else
120  dest[ d ] = src[ m++ ];
121  }
122  }
123  catch ( Exception e ) {
124  }
125 }
126 
127 template <class T> void DynaSortArray<T>::mergeSort( T* array, int length, int low, int high, IDynaComparator<T>& comparator ) {
128  if ( array != nullptr && length > 1 && low >= 0 && high < length ) {
129  auto copy = DynaAllocArray<T>::newArray(length);
130  memcpy(copy, array, length * sizeof(T));
131  _mergeSort( copy, array, low, high + 1, comparator );
132  delete copy;
133  }
134 }
135 
136 template <class T> void DynaSortArray<T>::mergeSort( T* array, int length, IDynaComparator<T>& comparator ) {
137  mergeSort( array, length, 0, length - 1, comparator );
138 }
139 
140 template <class T> void DynaSortArray<T>::mergeSort( DynaArray<T>* array, int low, int high, IDynaComparator<T>& comparator ) {
141  if ( array != nullptr )
142  mergeSort( array->getInternalTypedArray(), array->count(), low, high, comparator );
143 }
144 
145 template <class T> void DynaSortArray<T>::mergeSort( DynaArray<T>* array, IDynaComparator<T>& comparator ) {
146  if ( array != nullptr )
147  mergeSort( array->getInternalTypedArray(), array->count(), 0, array->count() - 1, comparator );
148 }
149 
150 #endif
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