jsorta.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. #ifndef JSORTARR_HPP
  15. #define JSORTARR_HPP
  16. #include "jexpdef.hpp"
  17. #include "jsort.hpp"
  18. /* the following somewhat inelegantly implements:
  19. template <class C>
  20. void qsortarray(C *a, size32_t n, sortCompareFunction compare, sortSwapFunction doswap)
  21. template <class C>
  22. void qsortarray(C *a, size32_t n, sortCompareFunction compare)
  23. template <class C>
  24. void qsortarray(C *a, size32_t n, const ICompare &compare, sortSwapFunction doswap)
  25. template <class C>
  26. void qsortarray(C *a, size32_t n, const ICompare &compare)
  27. template <class C>
  28. void qsortarray(C *a, size32_t n)
  29. */
  30. #define CMP(a,b) (compare((a),(b)))
  31. #define MED3(a,b,c) ((C *)med3ca(a,b,c,compare))
  32. #define RECURSE(a,b) qsortarray(a, b, compare, doswap)
  33. static inline void *med3ca(void *a, void *b, void *c, sortCompareFunction compare)
  34. { return CMP(a, b) < 0 ? (CMP(b, c) < 0 ? b : (CMP(a, c) < 0 ? c : a )) : (CMP(b, c) > 0 ? b : (CMP(a, c) < 0 ? a : c )); }
  35. #define VECTOR C*
  36. #define SWAP(a,b) doswap(a,b)
  37. template <class C>
  38. void qsortarray(C *a, size32_t n, sortCompareFunction compare, sortSwapFunction doswap)
  39. #include "jsort2.inc"
  40. #undef SWAP
  41. #define SWAP(a,b) { C t = *(a); *(a) = *(b); *(b) = t; }
  42. #undef RECURSE
  43. #define RECURSE(a,b) qsortarray(a, b, compare)
  44. template <class C>
  45. void qsortarray(C *a, size32_t n, sortCompareFunction compare)
  46. #include "jsort2.inc"
  47. #undef SWAP
  48. #define SWAP(a,b) doswap(a,b)
  49. #undef CMP
  50. #define CMP(a,b) (compare.docompare((a),(b)))
  51. #undef RECURSE
  52. #define RECURSE(a,b) qsortarray(a, b, compare, doswap)
  53. #undef MED3
  54. static inline void *med3cac(void *a, void *b, void *c, const ICompare &compare)
  55. { return CMP(a, b) < 0 ? (CMP(b, c) < 0 ? b : (CMP(a, c) < 0 ? c : a )) : (CMP(b, c) > 0 ? b : (CMP(a, c) < 0 ? a : c )); }
  56. #define MED3(a,b,c) ((C*)med3cac(a,b,c,compare))
  57. template <class C>
  58. void qsortarray(C *a, size32_t n, const ICompare &compare, sortSwapFunction doswap)
  59. #include "jsort2.inc"
  60. #undef SWAP
  61. #define SWAP(a,b) { C t = *(a); *(a) = *(b); *(b) = t; }
  62. #undef RECURSE
  63. #define RECURSE(a,b) qsortarray(a, b, compare)
  64. template <class C>
  65. void qsortarray(C *a, size32_t n, const ICompare &compare)
  66. #include "jsort2.inc"
  67. #undef CMP
  68. #define CMP(a,b) ((*(a)<*(b))?-1:((*(a)>*(b))?1:0))
  69. #undef RECURSE
  70. #define RECURSE(a,b) qsortarray(a, b)
  71. #undef MED3
  72. template <class C>
  73. static inline C *med3cas(C *a, C *b, C *c)
  74. { return *a<*b ? (*b<*c ? b : (*a<*c ? c : a )) : (*b>*c ? b : (*a<*c ? a : c )); }
  75. #define MED3(a,b,c) med3cas<C>(a,b,c)
  76. template <class C>
  77. void qsortarray(C *a, size32_t n)
  78. #include "jsort2.inc"
  79. #undef CMP
  80. #undef MED3
  81. #undef RECURSE
  82. #undef SWAP
  83. #undef VECTOR
  84. #endif