jsorta.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef JSORTARR_HPP
  14. #define JSORTARR_HPP
  15. #include "jiface.hpp"
  16. #include "jsort.hpp"
  17. /* the following somewhat inelegantly implements:
  18. template <class C>
  19. void qsortarray(C *a, size32_t n, sortCompareFunction compare, sortSwapFunction doswap)
  20. template <class C>
  21. void qsortarray(C *a, size32_t n, sortCompareFunction compare)
  22. template <class C>
  23. void qsortarray(C *a, size32_t n, const ICompare &compare, sortSwapFunction doswap)
  24. template <class C>
  25. void qsortarray(C *a, size32_t n, const ICompare &compare)
  26. template <class C>
  27. void qsortarray(C *a, size32_t n)
  28. */
  29. #define CMP(a,b) (compare((a),(b)))
  30. #define MED3(a,b,c) ((C *)med3ca(a,b,c,compare))
  31. #define RECURSE(a,b) qsortarray(a, b, compare, doswap)
  32. static inline void *med3ca(void *a, void *b, void *c, sortCompareFunction compare)
  33. { 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 )); }
  34. #define VECTOR C*
  35. #define SWAP(a,b) doswap(a,b)
  36. template <class C>
  37. void qsortarray(C *a, size32_t n, sortCompareFunction compare, sortSwapFunction doswap)
  38. #include "jsort2.inc"
  39. #undef SWAP
  40. #define SWAP(a,b) { C t = *(a); *(a) = *(b); *(b) = t; }
  41. #undef RECURSE
  42. #define RECURSE(a,b) qsortarray(a, b, compare)
  43. template <class C>
  44. void qsortarray(C *a, size32_t n, sortCompareFunction compare)
  45. #include "jsort2.inc"
  46. #undef SWAP
  47. #define SWAP(a,b) doswap(a,b)
  48. #undef CMP
  49. #define CMP(a,b) (compare.docompare((a),(b)))
  50. #undef RECURSE
  51. #define RECURSE(a,b) qsortarray(a, b, compare, doswap)
  52. #undef MED3
  53. static inline void *med3cac(void *a, void *b, void *c, const ICompare &compare)
  54. { 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 )); }
  55. #define MED3(a,b,c) ((C*)med3cac(a,b,c,compare))
  56. template <class C>
  57. void qsortarray(C *a, size32_t n, const ICompare &compare, sortSwapFunction doswap)
  58. #include "jsort2.inc"
  59. #undef SWAP
  60. #define SWAP(a,b) { C t = *(a); *(a) = *(b); *(b) = t; }
  61. #undef RECURSE
  62. #define RECURSE(a,b) qsortarray(a, b, compare)
  63. template <class C>
  64. void qsortarray(C *a, size32_t n, const ICompare &compare)
  65. #include "jsort2.inc"
  66. #undef CMP
  67. #define CMP(a,b) ((*(a)<*(b))?-1:((*(a)>*(b))?1:0))
  68. #undef RECURSE
  69. #define RECURSE(a,b) qsortarray(a, b)
  70. #undef MED3
  71. template <class C>
  72. static inline C *med3cas(C *a, C *b, C *c)
  73. { return *a<*b ? (*b<*c ? b : (*a<*c ? c : a )) : (*b>*c ? b : (*a<*c ? a : c )); }
  74. #define MED3(a,b,c) med3cas<C>(a,b,c)
  75. template <class C>
  76. void qsortarray(C *a, size32_t n)
  77. #include "jsort2.inc"
  78. #undef CMP
  79. #undef MED3
  80. #undef RECURSE
  81. #undef SWAP
  82. #undef VECTOR
  83. #endif