ucharstriebuilder.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. *******************************************************************************
  3. * Copyright (C) 2010-2016, International Business Machines
  4. * Corporation and others. All Rights Reserved.
  5. *******************************************************************************
  6. * file name: ucharstriebuilder.h
  7. * encoding: US-ASCII
  8. * tab size: 8 (not used)
  9. * indentation:4
  10. *
  11. * created on: 2010nov14
  12. * created by: Markus W. Scherer
  13. */
  14. #ifndef __UCHARSTRIEBUILDER_H__
  15. #define __UCHARSTRIEBUILDER_H__
  16. #include "unicode/utypes.h"
  17. #include "unicode/stringtriebuilder.h"
  18. #include "unicode/ucharstrie.h"
  19. #include "unicode/unistr.h"
  20. /**
  21. * \file
  22. * \brief C++ API: Builder for icu::UCharsTrie
  23. */
  24. U_NAMESPACE_BEGIN
  25. class UCharsTrieElement;
  26. /**
  27. * Builder class for UCharsTrie.
  28. *
  29. * This class is not intended for public subclassing.
  30. * @stable ICU 4.8
  31. */
  32. class U_COMMON_API UCharsTrieBuilder : public StringTrieBuilder {
  33. public:
  34. /**
  35. * Constructs an empty builder.
  36. * @param errorCode Standard ICU error code.
  37. * @stable ICU 4.8
  38. */
  39. UCharsTrieBuilder(UErrorCode &errorCode);
  40. /**
  41. * Destructor.
  42. * @stable ICU 4.8
  43. */
  44. virtual ~UCharsTrieBuilder();
  45. /**
  46. * Adds a (string, value) pair.
  47. * The string must be unique.
  48. * The string contents will be copied; the builder does not keep
  49. * a reference to the input UnicodeString or its buffer.
  50. * @param s The input string.
  51. * @param value The value associated with this string.
  52. * @param errorCode Standard ICU error code. Its input value must
  53. * pass the U_SUCCESS() test, or else the function returns
  54. * immediately. Check for U_FAILURE() on output or use with
  55. * function chaining. (See User Guide for details.)
  56. * @return *this
  57. * @stable ICU 4.8
  58. */
  59. UCharsTrieBuilder &add(const UnicodeString &s, int32_t value, UErrorCode &errorCode);
  60. /**
  61. * Builds a UCharsTrie for the add()ed data.
  62. * Once built, no further data can be add()ed until clear() is called.
  63. *
  64. * A UCharsTrie cannot be empty. At least one (string, value) pair
  65. * must have been add()ed.
  66. *
  67. * This method passes ownership of the builder's internal result array to the new trie object.
  68. * Another call to any build() variant will re-serialize the trie.
  69. * After clear() has been called, a new array will be used as well.
  70. * @param buildOption Build option, see UStringTrieBuildOption.
  71. * @param errorCode Standard ICU error code. Its input value must
  72. * pass the U_SUCCESS() test, or else the function returns
  73. * immediately. Check for U_FAILURE() on output or use with
  74. * function chaining. (See User Guide for details.)
  75. * @return A new UCharsTrie for the add()ed data.
  76. * @stable ICU 4.8
  77. */
  78. UCharsTrie *build(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
  79. /**
  80. * Builds a UCharsTrie for the add()ed data and UChar-serializes it.
  81. * Once built, no further data can be add()ed until clear() is called.
  82. *
  83. * A UCharsTrie cannot be empty. At least one (string, value) pair
  84. * must have been add()ed.
  85. *
  86. * Multiple calls to buildUnicodeString() set the UnicodeStrings to the
  87. * builder's same UChar array, without rebuilding.
  88. * If buildUnicodeString() is called after build(), the trie will be
  89. * re-serialized into a new array.
  90. * If build() is called after buildUnicodeString(), the trie object will become
  91. * the owner of the previously returned array.
  92. * After clear() has been called, a new array will be used as well.
  93. * @param buildOption Build option, see UStringTrieBuildOption.
  94. * @param result A UnicodeString which will be set to the UChar-serialized
  95. * UCharsTrie for the add()ed data.
  96. * @param errorCode Standard ICU error code. Its input value must
  97. * pass the U_SUCCESS() test, or else the function returns
  98. * immediately. Check for U_FAILURE() on output or use with
  99. * function chaining. (See User Guide for details.)
  100. * @return result
  101. * @stable ICU 4.8
  102. */
  103. UnicodeString &buildUnicodeString(UStringTrieBuildOption buildOption, UnicodeString &result,
  104. UErrorCode &errorCode);
  105. /**
  106. * Removes all (string, value) pairs.
  107. * New data can then be add()ed and a new trie can be built.
  108. * @return *this
  109. * @stable ICU 4.8
  110. */
  111. UCharsTrieBuilder &clear() {
  112. strings.remove();
  113. elementsLength=0;
  114. ucharsLength=0;
  115. return *this;
  116. }
  117. private:
  118. UCharsTrieBuilder(const UCharsTrieBuilder &other); // no copy constructor
  119. UCharsTrieBuilder &operator=(const UCharsTrieBuilder &other); // no assignment operator
  120. void buildUChars(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
  121. virtual int32_t getElementStringLength(int32_t i) const;
  122. virtual UChar getElementUnit(int32_t i, int32_t unitIndex) const;
  123. virtual int32_t getElementValue(int32_t i) const;
  124. virtual int32_t getLimitOfLinearMatch(int32_t first, int32_t last, int32_t unitIndex) const;
  125. virtual int32_t countElementUnits(int32_t start, int32_t limit, int32_t unitIndex) const;
  126. virtual int32_t skipElementsBySomeUnits(int32_t i, int32_t unitIndex, int32_t count) const;
  127. virtual int32_t indexOfElementWithNextUnit(int32_t i, int32_t unitIndex, UChar unit) const;
  128. virtual UBool matchNodesCanHaveValues() const { return TRUE; }
  129. virtual int32_t getMaxBranchLinearSubNodeLength() const { return UCharsTrie::kMaxBranchLinearSubNodeLength; }
  130. virtual int32_t getMinLinearMatch() const { return UCharsTrie::kMinLinearMatch; }
  131. virtual int32_t getMaxLinearMatchLength() const { return UCharsTrie::kMaxLinearMatchLength; }
  132. class UCTLinearMatchNode : public LinearMatchNode {
  133. public:
  134. UCTLinearMatchNode(const UChar *units, int32_t len, Node *nextNode);
  135. virtual UBool operator==(const Node &other) const;
  136. virtual void write(StringTrieBuilder &builder);
  137. private:
  138. const UChar *s;
  139. };
  140. virtual Node *createLinearMatchNode(int32_t i, int32_t unitIndex, int32_t length,
  141. Node *nextNode) const;
  142. UBool ensureCapacity(int32_t length);
  143. virtual int32_t write(int32_t unit);
  144. int32_t write(const UChar *s, int32_t length);
  145. virtual int32_t writeElementUnits(int32_t i, int32_t unitIndex, int32_t length);
  146. virtual int32_t writeValueAndFinal(int32_t i, UBool isFinal);
  147. virtual int32_t writeValueAndType(UBool hasValue, int32_t value, int32_t node);
  148. virtual int32_t writeDeltaTo(int32_t jumpTarget);
  149. UnicodeString strings;
  150. UCharsTrieElement *elements;
  151. int32_t elementsCapacity;
  152. int32_t elementsLength;
  153. // UChar serialization of the trie.
  154. // Grows from the back: ucharsLength measures from the end of the buffer!
  155. UChar *uchars;
  156. int32_t ucharsCapacity;
  157. int32_t ucharsLength;
  158. };
  159. U_NAMESPACE_END
  160. #endif // __UCHARSTRIEBUILDER_H__