jsort.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 JSORT_HPP
  14. #define JSORT_HPP
  15. #include "jiface.hpp"
  16. #include "jio.hpp"
  17. #ifndef ICOMPARE_DEFINED
  18. #define ICOMPARE_DEFINED
  19. struct ICompare
  20. {
  21. virtual int docompare(const void *,const void *) const =0;
  22. protected:
  23. virtual ~ICompare() {}
  24. };
  25. #endif
  26. // useful binary insertion function used by array functions.
  27. typedef int (*sortCompareFunction)(const void * left, const void * right);
  28. extern jlib_decl void * binary_add(const void *newitem, const void *base,
  29. size32_t nmemb,
  30. size32_t width,
  31. sortCompareFunction compare,
  32. bool * ItemAdded);
  33. extern jlib_decl void * binary_vec_find(const void *search, const void * *base,
  34. size32_t nmemb,
  35. sortCompareFunction compare,
  36. bool * isNew);
  37. extern jlib_decl void * binary_vec_find(const void *search, const void * *base,
  38. size32_t nmemb,
  39. ICompare & compare,
  40. bool * isNew);
  41. extern jlib_decl void * binary_vec_insert(const void *newitem, const void * *base,
  42. size32_t nmemb,
  43. sortCompareFunction compare);
  44. extern jlib_decl void * binary_vec_insert(const void *newitem, const void * *base,
  45. size32_t nmemb,
  46. ICompare const & compare);
  47. extern jlib_decl void * binary_vec_insert_stable(const void *newitem, const void * *base,
  48. size32_t nmemb,
  49. sortCompareFunction compare);
  50. extern jlib_decl void * binary_vec_insert_stable(const void *newitem, const void * *base,
  51. size32_t nmemb,
  52. ICompare const & compare);
  53. extern jlib_decl void qsortvec(void **a, size32_t n, size32_t es);
  54. extern jlib_decl void qsortvec(void **a, size32_t n, sortCompareFunction compare);
  55. extern jlib_decl void qsortvec(void **a, size32_t n, const ICompare & compare);
  56. extern jlib_decl void qsortvec(void **a, size32_t n, const ICompare & compare1, const ICompare & compare2);
  57. // Call with n rows of data in rows, index an (uninitialized) array of size n. The function will fill index with a stably sorted index into rows.
  58. extern jlib_decl void qsortvecstableinplace(void ** rows, size32_t n, const ICompare & compare, void ** temp);
  59. extern jlib_decl void parqsortvec(void **a, size32_t n, const ICompare & compare, unsigned ncpus=0); // runs in parallel on multi-core
  60. extern jlib_decl void parqsortvecstableinplace(void ** rows, size32_t n, const ICompare & compare, void ** temp, unsigned ncpus=0); // runs in parallel on multi-core
  61. // we define the heap property that no element c should be smaller than its parent (unsigned)(c-1)/2
  62. // heap stores indexes into the data in rows, so compare->docompare is called with arguments rows[heap[i]]
  63. // these functions are stable
  64. // assuming that all elements >p form a heap, this function sifts p down to its correct position, and so includes it in the heap; it returns true if no change is made
  65. extern jlib_decl bool heap_push_down(unsigned p, unsigned num, unsigned * heap, const void ** rows, ICompare * compare);
  66. // assuming that all elements <c form a heap, this function pushes c up to its correct position; it returns true if no change is made
  67. extern jlib_decl bool heap_push_up(unsigned c, unsigned * heap, const void ** rows, ICompare * compare);
  68. extern jlib_decl IRowStream *createRowStreamMerger(unsigned numstreams,IRowProvider &provider,ICompare *icmp, bool partdedup=false);
  69. extern jlib_decl IRowStream *createRowStreamMerger(unsigned numstreams,IRowStream **instreams,ICompare *icmp, bool partdedup, IRowLinkCounter *linkcounter);
  70. class ISortedRowProvider
  71. {
  72. public:
  73. virtual void * getNextSorted() = 0;
  74. };
  75. #define MIN(a, b) ((a) < (b) ? a : b)
  76. typedef void (*sortSwapFunction)(void * left, void * right);
  77. // see jsorta.hpp for array sorting routines
  78. #endif