bytestriebuilder.h 7.1 KB

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