rtlrank.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #include "platform.h"
  14. #include <math.h>
  15. #include <stdio.h>
  16. #include "jmisc.hpp"
  17. #include "jutil.hpp"
  18. #include "jsort.hpp"
  19. #include "jlib.hpp"
  20. #include "eclrtl.hpp"
  21. //=============================================================================
  22. // Miscellaneous string functions...
  23. void rtlCreateOrder(void * tgt, const void * src, unsigned num, unsigned width, const void * compare)
  24. {
  25. //create an array of pointers to the objects...
  26. void * * vector = (void * *)malloc(num * sizeof(void *));
  27. unsigned idx;
  28. char * finger = (char *)src;
  29. for (idx = 0; idx < num; idx++)
  30. {
  31. vector[idx] = finger;
  32. finger += width;
  33. }
  34. //sort the elements.
  35. qsortvec(vector, num, (sortCompareFunction)compare);
  36. //now calculate the indices...
  37. unsigned * indices = (unsigned *)tgt;
  38. for (idx = 0; idx < num; idx++)
  39. indices[idx] = ((char *)vector[idx] - (char *)src) / width + 1; // NB: 1 based...
  40. free(vector);
  41. }
  42. unsigned rtlRankFromOrder(unsigned index, unsigned num, const void * order)
  43. {
  44. const unsigned * indices = (const unsigned *)order;
  45. unsigned idx;
  46. for (idx =0; idx < num; idx++)
  47. if (indices[idx] == index)
  48. return idx+1;
  49. return 0;
  50. }
  51. unsigned rtlRankedFromOrder(unsigned index, unsigned num, const void * order)
  52. {
  53. const unsigned * indices = (const unsigned *)order;
  54. if ((index < 1) || (index > num))
  55. return 0;
  56. return indices[index-1];
  57. }