sort_func.hpp
Go to the documentation of this file.00001
00002
00005 #ifndef SORT_FUNC_HPP
00006 #define SORT_FUNC_HPP
00007
00008 #include <stdlib.h>
00009 #include "math_func.hpp"
00010 #include "mem_func.hpp"
00011
00023 template <typename T>
00024 static FORCEINLINE void QSortT(T *base, uint num, int (CDECL *comparator)(const T*, const T*), bool desc = false)
00025 {
00026 if (num < 2) return;
00027
00028 qsort(base, num, sizeof(T), (int (CDECL *)(const void *, const void *))comparator);
00029
00030 if (desc) MemReverseT(base, num);
00031 }
00032
00047 template <typename T>
00048 static inline void GSortT(T *base, uint num, int (CDECL *comparator)(const T*, const T*), bool desc = false)
00049 {
00050 if (num < 2) return;
00051
00052 assert(base != NULL);
00053 assert(comparator != NULL);
00054
00055 T *a = base;
00056 T *b = base + 1;
00057 uint offset = 0;
00058
00059 while (num > 1) {
00060 const int diff = comparator(a, b);
00061 if ((!desc && diff <= 0) || (desc && diff >= 0)) {
00062 if (offset != 0) {
00063
00064 a += offset;
00065 b += offset;
00066 offset = 0;
00067 continue;
00068 }
00069
00070 a++;
00071 b++;
00072 num--;
00073 } else {
00074 Swap(*a, *b);
00075
00076 if (a == base) continue;
00077
00078 a--;
00079 b--;
00080 offset++;
00081 }
00082 }
00083 }
00084
00085 #endif