ucharstriebuilder.h 7.2 KB

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