ucharstriebuilder.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 (because build() passes on ownership).
  93. * If build() is called after buildUnicodeString(), the trie object returned
  94. * by build() will become the owner of the underlying data for the
  95. * previously returned UnicodeString.
  96. * After clear() has been called, a new array will be used as well.
  97. * @param buildOption Build option, see UStringTrieBuildOption.
  98. * @param result A UnicodeString which will be set to the char16_t-serialized
  99. * UCharsTrie for the add()ed data.
  100. * @param errorCode Standard ICU error code. Its input value must
  101. * pass the U_SUCCESS() test, or else the function returns
  102. * immediately. Check for U_FAILURE() on output or use with
  103. * function chaining. (See User Guide for details.)
  104. * @return result
  105. * @stable ICU 4.8
  106. */
  107. UnicodeString &buildUnicodeString(UStringTrieBuildOption buildOption, UnicodeString &result,
  108. UErrorCode &errorCode);
  109. /**
  110. * Removes all (string, value) pairs.
  111. * New data can then be add()ed and a new trie can be built.
  112. * @return *this
  113. * @stable ICU 4.8
  114. */
  115. UCharsTrieBuilder &clear() {
  116. strings.remove();
  117. elementsLength=0;
  118. ucharsLength=0;
  119. return *this;
  120. }
  121. private:
  122. UCharsTrieBuilder(const UCharsTrieBuilder &other) = delete; // no copy constructor
  123. UCharsTrieBuilder &operator=(const UCharsTrieBuilder &other) = delete; // no assignment operator
  124. void buildUChars(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
  125. virtual int32_t getElementStringLength(int32_t i) const override;
  126. virtual char16_t getElementUnit(int32_t i, int32_t unitIndex) const override;
  127. virtual int32_t getElementValue(int32_t i) const override;
  128. virtual int32_t getLimitOfLinearMatch(int32_t first, int32_t last, int32_t unitIndex) const override;
  129. virtual int32_t countElementUnits(int32_t start, int32_t limit, int32_t unitIndex) const override;
  130. virtual int32_t skipElementsBySomeUnits(int32_t i, int32_t unitIndex, int32_t count) const override;
  131. virtual int32_t indexOfElementWithNextUnit(int32_t i, int32_t unitIndex, char16_t unit) const override;
  132. virtual UBool matchNodesCanHaveValues() const override { return true; }
  133. virtual int32_t getMaxBranchLinearSubNodeLength() const override { return UCharsTrie::kMaxBranchLinearSubNodeLength; }
  134. virtual int32_t getMinLinearMatch() const override { return UCharsTrie::kMinLinearMatch; }
  135. virtual int32_t getMaxLinearMatchLength() const override { return UCharsTrie::kMaxLinearMatchLength; }
  136. class UCTLinearMatchNode : public LinearMatchNode {
  137. public:
  138. UCTLinearMatchNode(const char16_t *units, int32_t len, Node *nextNode);
  139. virtual bool operator==(const Node &other) const override;
  140. virtual void write(StringTrieBuilder &builder) override;
  141. private:
  142. const char16_t *s;
  143. };
  144. virtual Node *createLinearMatchNode(int32_t i, int32_t unitIndex, int32_t length,
  145. Node *nextNode) const override;
  146. UBool ensureCapacity(int32_t length);
  147. virtual int32_t write(int32_t unit) override;
  148. int32_t write(const char16_t *s, int32_t length);
  149. virtual int32_t writeElementUnits(int32_t i, int32_t unitIndex, int32_t length) override;
  150. virtual int32_t writeValueAndFinal(int32_t i, UBool isFinal) override;
  151. virtual int32_t writeValueAndType(UBool hasValue, int32_t value, int32_t node) override;
  152. virtual int32_t writeDeltaTo(int32_t jumpTarget) override;
  153. UnicodeString strings;
  154. UCharsTrieElement *elements;
  155. int32_t elementsCapacity;
  156. int32_t elementsLength;
  157. // char16_t serialization of the trie.
  158. // Grows from the back: ucharsLength measures from the end of the buffer!
  159. char16_t *uchars;
  160. int32_t ucharsCapacity;
  161. int32_t ucharsLength;
  162. };
  163. U_NAMESPACE_END
  164. #endif /* U_SHOW_CPLUSPLUS_API */
  165. #endif // __UCHARSTRIEBUILDER_H__