bytestriebuilder.h 7.1 KB

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