uarrsort.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. *
  6. * Copyright (C) 2003-2013, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. *******************************************************************************
  10. * file name: uarrsort.h
  11. * encoding: UTF-8
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 2003aug04
  16. * created by: Markus W. Scherer
  17. *
  18. * Internal function for sorting arrays.
  19. */
  20. #ifndef __UARRSORT_H__
  21. #define __UARRSORT_H__
  22. #include "unicode/utypes.h"
  23. U_CDECL_BEGIN
  24. /**
  25. * Function type for comparing two items as part of sorting an array or similar.
  26. * Callback function for uprv_sortArray().
  27. *
  28. * @param context Application-specific pointer, passed through by uprv_sortArray().
  29. * @param left Pointer to the "left" item.
  30. * @param right Pointer to the "right" item.
  31. * @return 32-bit signed integer comparison result:
  32. * <0 if left<right
  33. * ==0 if left==right
  34. * >0 if left>right
  35. *
  36. * @internal
  37. */
  38. typedef int32_t U_CALLCONV
  39. UComparator(const void *context, const void *left, const void *right);
  40. U_CDECL_END
  41. /**
  42. * Array sorting function.
  43. * Uses a UComparator for comparing array items to each other, and simple
  44. * memory copying to move items.
  45. *
  46. * @param array The array to be sorted.
  47. * @param length The number of items in the array.
  48. * @param itemSize The size in bytes of each array item.
  49. * @param cmp UComparator function used to compare two items each.
  50. * @param context Application-specific pointer, passed through to the UComparator.
  51. * @param sortStable If true, a stable sorting algorithm must be used.
  52. * @param pErrorCode ICU in/out UErrorCode parameter.
  53. *
  54. * @internal
  55. */
  56. U_CAPI void U_EXPORT2
  57. uprv_sortArray(void *array, int32_t length, int32_t itemSize,
  58. UComparator *cmp, const void *context,
  59. UBool sortStable, UErrorCode *pErrorCode);
  60. /**
  61. * Convenience UComparator implementation for uint16_t arrays.
  62. * @internal
  63. */
  64. U_CAPI int32_t U_EXPORT2
  65. uprv_uint16Comparator(const void *context, const void *left, const void *right);
  66. /**
  67. * Convenience UComparator implementation for int32_t arrays.
  68. * @internal
  69. */
  70. U_CAPI int32_t U_EXPORT2
  71. uprv_int32Comparator(const void *context, const void *left, const void *right);
  72. /**
  73. * Convenience UComparator implementation for uint32_t arrays.
  74. * @internal
  75. */
  76. U_CAPI int32_t U_EXPORT2
  77. uprv_uint32Comparator(const void *context, const void *left, const void *right);
  78. /**
  79. * Much like Java Collections.binarySearch(list, key, comparator).
  80. *
  81. * Except: Java documents "If the list contains multiple elements equal to
  82. * the specified object, there is no guarantee which one will be found."
  83. *
  84. * This version here will return the largest index of any equal item,
  85. * for use in stable sorting.
  86. *
  87. * @return the index>=0 where the item was found:
  88. * the largest such index, if multiple, for stable sorting;
  89. * or the index<0 for inserting the item at ~index in sorted order
  90. */
  91. U_CAPI int32_t U_EXPORT2
  92. uprv_stableBinarySearch(char *array, int32_t length, void *item, int32_t itemSize,
  93. UComparator *cmp, const void *context);
  94. #endif