collationweights.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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) 1999-2014, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. *******************************************************************************
  10. * file name: collationweights.h
  11. * encoding: UTF-8
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 2001mar08 as ucol_wgt.h
  16. * created by: Markus W. Scherer
  17. */
  18. #ifndef __COLLATIONWEIGHTS_H__
  19. #define __COLLATIONWEIGHTS_H__
  20. #include "unicode/utypes.h"
  21. #if !UCONFIG_NO_COLLATION
  22. #include "unicode/uobject.h"
  23. U_NAMESPACE_BEGIN
  24. /**
  25. * Allocates n collation element weights between two exclusive limits.
  26. * Used only internally by the collation tailoring builder.
  27. */
  28. class U_I18N_API CollationWeights : public UMemory {
  29. public:
  30. CollationWeights();
  31. static inline int32_t lengthOfWeight(uint32_t weight) {
  32. if((weight&0xffffff)==0) {
  33. return 1;
  34. } else if((weight&0xffff)==0) {
  35. return 2;
  36. } else if((weight&0xff)==0) {
  37. return 3;
  38. } else {
  39. return 4;
  40. }
  41. }
  42. void initForPrimary(UBool compressible);
  43. void initForSecondary();
  44. void initForTertiary();
  45. /**
  46. * Determine heuristically
  47. * what ranges to use for a given number of weights between (excluding)
  48. * two limits.
  49. *
  50. * @param lowerLimit A collation element weight; the ranges will be filled to cover
  51. * weights greater than this one.
  52. * @param upperLimit A collation element weight; the ranges will be filled to cover
  53. * weights less than this one.
  54. * @param n The number of collation element weights w necessary such that
  55. * lowerLimit<w<upperLimit in lexical order.
  56. * @return TRUE if it is possible to fit n elements between the limits
  57. */
  58. UBool allocWeights(uint32_t lowerLimit, uint32_t upperLimit, int32_t n);
  59. /**
  60. * Given a set of ranges calculated by allocWeights(),
  61. * iterate through the weights.
  62. * The ranges are modified to keep the current iteration state.
  63. *
  64. * @return The next weight in the ranges, or 0xffffffff if there is none left.
  65. */
  66. uint32_t nextWeight();
  67. /** @internal */
  68. struct WeightRange {
  69. uint32_t start, end;
  70. int32_t length, count;
  71. };
  72. private:
  73. /** @return number of usable byte values for byte idx */
  74. inline int32_t countBytes(int32_t idx) const {
  75. return (int32_t)(maxBytes[idx] - minBytes[idx] + 1);
  76. }
  77. uint32_t incWeight(uint32_t weight, int32_t length) const;
  78. uint32_t incWeightByOffset(uint32_t weight, int32_t length, int32_t offset) const;
  79. void lengthenRange(WeightRange &range) const;
  80. /**
  81. * Takes two CE weights and calculates the
  82. * possible ranges of weights between the two limits, excluding them.
  83. * For weights with up to 4 bytes there are up to 2*4-1=7 ranges.
  84. */
  85. UBool getWeightRanges(uint32_t lowerLimit, uint32_t upperLimit);
  86. UBool allocWeightsInShortRanges(int32_t n, int32_t minLength);
  87. UBool allocWeightsInMinLengthRanges(int32_t n, int32_t minLength);
  88. int32_t middleLength;
  89. uint32_t minBytes[5]; // for byte 1, 2, 3, 4
  90. uint32_t maxBytes[5];
  91. WeightRange ranges[7];
  92. int32_t rangeIndex;
  93. int32_t rangeCount;
  94. };
  95. U_NAMESPACE_END
  96. #endif // !UCONFIG_NO_COLLATION
  97. #endif // __COLLATIONWEIGHTS_H__