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