bytestriebuilder.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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: bytestriebuilder.h
  9. * encoding: US-ASCII
  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. #include "unicode/bytestrie.h"
  24. #include "unicode/stringpiece.h"
  25. #include "unicode/stringtriebuilder.h"
  26. U_NAMESPACE_BEGIN
  27. class BytesTrieElement;
  28. class CharString;
  29. /**
  30. * Builder class for BytesTrie.
  31. *
  32. * This class is not intended for public subclassing.
  33. * @stable ICU 4.8
  34. */
  35. class U_COMMON_API BytesTrieBuilder : public StringTrieBuilder {
  36. public:
  37. /**
  38. * Constructs an empty builder.
  39. * @param errorCode Standard ICU error code.
  40. * @stable ICU 4.8
  41. */
  42. BytesTrieBuilder(UErrorCode &errorCode);
  43. /**
  44. * Destructor.
  45. * @stable ICU 4.8
  46. */
  47. virtual ~BytesTrieBuilder();
  48. /**
  49. * Adds a (byte sequence, value) pair.
  50. * The byte sequence must be unique.
  51. * The bytes will be copied; the builder does not keep
  52. * a reference to the input StringPiece or its data().
  53. * @param s The input byte sequence.
  54. * @param value The value associated with this byte sequence.
  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. BytesTrieBuilder &add(StringPiece s, int32_t value, UErrorCode &errorCode);
  63. /**
  64. * Builds a BytesTrie for the add()ed data.
  65. * Once built, no further data can be add()ed until clear() is called.
  66. *
  67. * A BytesTrie cannot be empty. At least one (byte sequence, 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 BytesTrie for the add()ed data.
  79. * @stable ICU 4.8
  80. */
  81. BytesTrie *build(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
  82. /**
  83. * Builds a BytesTrie for the add()ed data and byte-serializes it.
  84. * Once built, no further data can be add()ed until clear() is called.
  85. *
  86. * A BytesTrie cannot be empty. At least one (byte sequence, value) pair
  87. * must have been add()ed.
  88. *
  89. * Multiple calls to buildStringPiece() return StringPieces referring to the
  90. * builder's same byte array, without rebuilding.
  91. * If buildStringPiece() is called after build(), the trie will be
  92. * re-serialized into a new array.
  93. * If build() is called after buildStringPiece(), 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 errorCode Standard ICU error code. Its input value must
  98. * pass the U_SUCCESS() test, or else the function returns
  99. * immediately. Check for U_FAILURE() on output or use with
  100. * function chaining. (See User Guide for details.)
  101. * @return A StringPiece which refers to the byte-serialized BytesTrie for the add()ed data.
  102. * @stable ICU 4.8
  103. */
  104. StringPiece buildStringPiece(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
  105. /**
  106. * Removes all (byte sequence, value) pairs.
  107. * New data can then be add()ed and a new trie can be built.
  108. * @return *this
  109. * @stable ICU 4.8
  110. */
  111. BytesTrieBuilder &clear();
  112. private:
  113. BytesTrieBuilder(const BytesTrieBuilder &other); // no copy constructor
  114. BytesTrieBuilder &operator=(const BytesTrieBuilder &other); // no assignment operator
  115. void buildBytes(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
  116. virtual int32_t getElementStringLength(int32_t i) const;
  117. virtual UChar getElementUnit(int32_t i, int32_t byteIndex) const;
  118. virtual int32_t getElementValue(int32_t i) const;
  119. virtual int32_t getLimitOfLinearMatch(int32_t first, int32_t last, int32_t byteIndex) const;
  120. virtual int32_t countElementUnits(int32_t start, int32_t limit, int32_t byteIndex) const;
  121. virtual int32_t skipElementsBySomeUnits(int32_t i, int32_t byteIndex, int32_t count) const;
  122. virtual int32_t indexOfElementWithNextUnit(int32_t i, int32_t byteIndex, UChar byte) const;
  123. virtual UBool matchNodesCanHaveValues() const { return FALSE; }
  124. virtual int32_t getMaxBranchLinearSubNodeLength() const { return BytesTrie::kMaxBranchLinearSubNodeLength; }
  125. virtual int32_t getMinLinearMatch() const { return BytesTrie::kMinLinearMatch; }
  126. virtual int32_t getMaxLinearMatchLength() const { return BytesTrie::kMaxLinearMatchLength; }
  127. /**
  128. * @internal
  129. */
  130. class BTLinearMatchNode : public LinearMatchNode {
  131. public:
  132. BTLinearMatchNode(const char *units, int32_t len, Node *nextNode);
  133. virtual UBool operator==(const Node &other) const;
  134. virtual void write(StringTrieBuilder &builder);
  135. private:
  136. const char *s;
  137. };
  138. // don't use #ifndef U_HIDE_INTERNAL_API with private class members or virtual methods.
  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 // __BYTESTRIEBUILDER_H__