Sort.h 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /******************************************************************************
  2. Use 'Sort' functions to sort custom data.
  3. Use 'BinarySearch' functions to perform binary search on custom data.
  4. /******************************************************************************/
  5. // Basic Compare Functions
  6. inline Int Compare(C SByte &a, C SByte &b) {if(a<b)return -1; if(a>b)return +1; return 0;} // compare 'a' 'b' values and return -1, 0, +1
  7. inline Int Compare(C Byte &a, C Byte &b) {if(a<b)return -1; if(a>b)return +1; return 0;} // compare 'a' 'b' values and return -1, 0, +1
  8. inline Int Compare(C Short &a, C Short &b) {if(a<b)return -1; if(a>b)return +1; return 0;} // compare 'a' 'b' values and return -1, 0, +1
  9. inline Int Compare(C UShort &a, C UShort &b) {if(a<b)return -1; if(a>b)return +1; return 0;} // compare 'a' 'b' values and return -1, 0, +1
  10. inline Int Compare(C Int &a, C Int &b) {if(a<b)return -1; if(a>b)return +1; return 0;} // compare 'a' 'b' values and return -1, 0, +1
  11. inline Int Compare(C UInt &a, C UInt &b) {if(a<b)return -1; if(a>b)return +1; return 0;} // compare 'a' 'b' values and return -1, 0, +1
  12. inline Int Compare(C Long &a, C Long &b) {if(a<b)return -1; if(a>b)return +1; return 0;} // compare 'a' 'b' values and return -1, 0, +1
  13. inline Int Compare(C ULong &a, C ULong &b) {if(a<b)return -1; if(a>b)return +1; return 0;} // compare 'a' 'b' values and return -1, 0, +1
  14. inline Int Compare(C Half &a, C Half &b) {if(a<b)return -1; if(a>b)return +1; return 0;} // compare 'a' 'b' values and return -1, 0, +1
  15. inline Int Compare(C Flt &a, C Flt &b) {if(a<b)return -1; if(a>b)return +1; return 0;} // compare 'a' 'b' values and return -1, 0, +1
  16. inline Int Compare(C Dbl &a, C Dbl &b) {if(a<b)return -1; if(a>b)return +1; return 0;} // compare 'a' 'b' values and return -1, 0, +1
  17. inline Int Compare(C Ptr &a, C Ptr &b) {if(a<b)return -1; if(a>b)return +1; return 0;} // compare 'a' 'b' values and return -1, 0, +1
  18. T1(TYPE) ENABLE_IF_ENUM(TYPE, Int) Compare(C TYPE &a, C TYPE &b) {if(a<b)return -1; if(a>b)return +1; return 0;} // compare 'a' 'b' enum values and return -1, 0, +1
  19. inline Int ComparePtr(CPtr a, CPtr b) {if(a<b)return -1; if(a>b)return +1; return 0;} // compare 'a' 'b' values and return -1, 0, +1
  20. inline Int CompareEps(C Flt &a, C Flt &b) {return SignEps(a-b);}
  21. inline Int Compare(C IndexWeight &a, C IndexWeight &b) {return Compare(b.weight, a.weight);} // compare in reversed order because we're sorting from most to least important
  22. // Sort Data
  23. void Sort(Int *data, Int elms ); // sort Int array
  24. void Sort(Flt *data, Int elms ); // sort Flt array
  25. void Sort(Dbl *data, Int elms ); // sort Dbl array
  26. T1(TYPE) void Sort(TYPE *data, Int elms, Int compare(C TYPE &a, C TYPE &b)=Compare); // sort custom array using custom comparing function
  27. // Binary Search, search sorted 'data' array for presence of 'value' and return if it was found in the array, 'index'=if the function returned true then this index points to the location where the 'value' is located in the array, if the function returned false then it means that 'value' was not found in the array however the 'index' points to the place where it should be added in the array while preserving sorted data, 'index' will always be in range (0..elms) inclusive
  28. T2(DATA, VALUE) Bool BinarySearch(C DATA *data, Int elms, C VALUE &value, Int &index, Int compare(C DATA &a, C VALUE &b)=Compare);
  29. T2(DATA, VALUE) Bool BinaryHas (C DATA *data, Int elms, C VALUE &value, Int compare(C DATA &a, C VALUE &b)=Compare) {Int i; return BinarySearch(data, elms, value, i, compare); } // check if 'value' is present in array
  30. T2(DATA, VALUE) DATA* BinaryFind ( DATA *data, Int elms, C VALUE &value, Int compare(C DATA &a, C VALUE &b)=Compare) {Int i; return BinarySearch(data, elms, value, i, compare) ? &data[i] : null;} // check if 'value' is present in array and return it, null on fail
  31. /******************************************************************************/