ucharstriebuilder.h 7.1 KB

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