1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2010-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: bytestriebuilder.h
10 * tab size: 8 (not used)
13 * created on: 2010sep25
14 * created by: Markus W. Scherer
19 * \brief C++ API: Builder for icu::BytesTrie
22 #ifndef __BYTESTRIEBUILDER_H__
23 #define __BYTESTRIEBUILDER_H__
25 #include "unicode/utypes.h"
26 #include "unicode/bytestrie.h"
27 #include "unicode/stringpiece.h"
28 #include "unicode/stringtriebuilder.h"
30 #if U_SHOW_CPLUSPLUS_API
33 class BytesTrieElement
;
36 * Builder class for BytesTrie.
38 * This class is not intended for public subclassing.
41 class U_COMMON_API BytesTrieBuilder
: public StringTrieBuilder
{
44 * Constructs an empty builder.
45 * @param errorCode Standard ICU error code.
48 BytesTrieBuilder(UErrorCode
&errorCode
);
54 virtual ~BytesTrieBuilder();
57 * Adds a (byte sequence, value) pair.
58 * The byte sequence must be unique.
59 * The bytes will be copied; the builder does not keep
60 * a reference to the input StringPiece or its data().
61 * @param s The input byte sequence.
62 * @param value The value associated with this byte sequence.
63 * @param errorCode Standard ICU error code. Its input value must
64 * pass the U_SUCCESS() test, or else the function returns
65 * immediately. Check for U_FAILURE() on output or use with
66 * function chaining. (See User Guide for details.)
70 BytesTrieBuilder
&add(StringPiece s
, int32_t value
, UErrorCode
&errorCode
);
73 * Builds a BytesTrie for the add()ed data.
74 * Once built, no further data can be add()ed until clear() is called.
76 * A BytesTrie cannot be empty. At least one (byte sequence, value) pair
77 * must have been add()ed.
79 * This method passes ownership of the builder's internal result array to the new trie object.
80 * Another call to any build() variant will re-serialize the trie.
81 * After clear() has been called, a new array will be used as well.
82 * @param buildOption Build option, see UStringTrieBuildOption.
83 * @param errorCode Standard ICU error code. Its input value must
84 * pass the U_SUCCESS() test, or else the function returns
85 * immediately. Check for U_FAILURE() on output or use with
86 * function chaining. (See User Guide for details.)
87 * @return A new BytesTrie for the add()ed data.
90 BytesTrie
*build(UStringTrieBuildOption buildOption
, UErrorCode
&errorCode
);
93 * Builds a BytesTrie for the add()ed data and byte-serializes it.
94 * Once built, no further data can be add()ed until clear() is called.
96 * A BytesTrie cannot be empty. At least one (byte sequence, value) pair
97 * must have been add()ed.
99 * Multiple calls to buildStringPiece() return StringPieces referring to the
100 * builder's same byte array, without rebuilding.
101 * If buildStringPiece() is called after build(), the trie will be
102 * re-serialized into a new array.
103 * If build() is called after buildStringPiece(), the trie object will become
104 * the owner of the previously returned array.
105 * After clear() has been called, a new array will be used as well.
106 * @param buildOption Build option, see UStringTrieBuildOption.
107 * @param errorCode Standard ICU error code. Its input value must
108 * pass the U_SUCCESS() test, or else the function returns
109 * immediately. Check for U_FAILURE() on output or use with
110 * function chaining. (See User Guide for details.)
111 * @return A StringPiece which refers to the byte-serialized BytesTrie for the add()ed data.
114 StringPiece
buildStringPiece(UStringTrieBuildOption buildOption
, UErrorCode
&errorCode
);
117 * Removes all (byte sequence, value) pairs.
118 * New data can then be add()ed and a new trie can be built.
122 BytesTrieBuilder
&clear();
125 BytesTrieBuilder(const BytesTrieBuilder
&other
); // no copy constructor
126 BytesTrieBuilder
&operator=(const BytesTrieBuilder
&other
); // no assignment operator
128 void buildBytes(UStringTrieBuildOption buildOption
, UErrorCode
&errorCode
);
130 virtual int32_t getElementStringLength(int32_t i
) const;
131 virtual char16_t getElementUnit(int32_t i
, int32_t byteIndex
) const;
132 virtual int32_t getElementValue(int32_t i
) const;
134 virtual int32_t getLimitOfLinearMatch(int32_t first
, int32_t last
, int32_t byteIndex
) const;
136 virtual int32_t countElementUnits(int32_t start
, int32_t limit
, int32_t byteIndex
) const;
137 virtual int32_t skipElementsBySomeUnits(int32_t i
, int32_t byteIndex
, int32_t count
) const;
138 virtual int32_t indexOfElementWithNextUnit(int32_t i
, int32_t byteIndex
, char16_t byte
) const;
140 virtual UBool
matchNodesCanHaveValues() const { return FALSE
; }
142 virtual int32_t getMaxBranchLinearSubNodeLength() const { return BytesTrie::kMaxBranchLinearSubNodeLength
; }
143 virtual int32_t getMinLinearMatch() const { return BytesTrie::kMinLinearMatch
; }
144 virtual int32_t getMaxLinearMatchLength() const { return BytesTrie::kMaxLinearMatchLength
; }
147 * @internal (private)
149 class BTLinearMatchNode
: public LinearMatchNode
{
151 BTLinearMatchNode(const char *units
, int32_t len
, Node
*nextNode
);
152 virtual UBool
operator==(const Node
&other
) const;
153 virtual void write(StringTrieBuilder
&builder
);
158 virtual Node
*createLinearMatchNode(int32_t i
, int32_t byteIndex
, int32_t length
,
159 Node
*nextNode
) const;
161 UBool
ensureCapacity(int32_t length
);
162 virtual int32_t write(int32_t byte
);
163 int32_t write(const char *b
, int32_t length
);
164 virtual int32_t writeElementUnits(int32_t i
, int32_t byteIndex
, int32_t length
);
165 virtual int32_t writeValueAndFinal(int32_t i
, UBool isFinal
);
166 virtual int32_t writeValueAndType(UBool hasValue
, int32_t value
, int32_t node
);
167 virtual int32_t writeDeltaTo(int32_t jumpTarget
);
169 CharString
*strings
; // Pointer not object so we need not #include internal charstr.h.
170 BytesTrieElement
*elements
;
171 int32_t elementsCapacity
;
172 int32_t elementsLength
;
174 // Byte serialization of the trie.
175 // Grows from the back: bytesLength measures from the end of the buffer!
177 int32_t bytesCapacity
;
182 #endif // U_SHOW_CPLUSPLUS_API
184 #endif // __BYTESTRIEBUILDER_H__