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"
27 #if U_SHOW_CPLUSPLUS_API
29 #include "unicode/bytestrie.h"
30 #include "unicode/stringpiece.h"
31 #include "unicode/stringtriebuilder.h"
35 class BytesTrieElement
;
38 * Builder class for BytesTrie.
40 * This class is not intended for public subclassing.
43 class U_COMMON_API BytesTrieBuilder
: public StringTrieBuilder
{
46 * Constructs an empty builder.
47 * @param errorCode Standard ICU error code.
50 BytesTrieBuilder(UErrorCode
&errorCode
);
56 virtual ~BytesTrieBuilder();
59 * Adds a (byte sequence, value) pair.
60 * The byte sequence must be unique.
61 * The bytes will be copied; the builder does not keep
62 * a reference to the input StringPiece or its data().
63 * @param s The input byte sequence.
64 * @param value The value associated with this byte sequence.
65 * @param errorCode Standard ICU error code. Its input value must
66 * pass the U_SUCCESS() test, or else the function returns
67 * immediately. Check for U_FAILURE() on output or use with
68 * function chaining. (See User Guide for details.)
72 BytesTrieBuilder
&add(StringPiece s
, int32_t value
, UErrorCode
&errorCode
);
75 * Builds a BytesTrie for the add()ed data.
76 * Once built, no further data can be add()ed until clear() is called.
78 * A BytesTrie cannot be empty. At least one (byte sequence, value) pair
79 * must have been add()ed.
81 * This method passes ownership of the builder's internal result array to the new trie object.
82 * Another call to any build() variant will re-serialize the trie.
83 * After clear() has been called, a new array will be used as well.
84 * @param buildOption Build option, see UStringTrieBuildOption.
85 * @param errorCode Standard ICU error code. Its input value must
86 * pass the U_SUCCESS() test, or else the function returns
87 * immediately. Check for U_FAILURE() on output or use with
88 * function chaining. (See User Guide for details.)
89 * @return A new BytesTrie for the add()ed data.
92 BytesTrie
*build(UStringTrieBuildOption buildOption
, UErrorCode
&errorCode
);
95 * Builds a BytesTrie for the add()ed data and byte-serializes it.
96 * Once built, no further data can be add()ed until clear() is called.
98 * A BytesTrie cannot be empty. At least one (byte sequence, value) pair
99 * must have been add()ed.
101 * Multiple calls to buildStringPiece() return StringPieces referring to the
102 * builder's same byte array, without rebuilding.
103 * If buildStringPiece() is called after build(), the trie will be
104 * re-serialized into a new array.
105 * If build() is called after buildStringPiece(), the trie object will become
106 * the owner of the previously returned array.
107 * After clear() has been called, a new array will be used as well.
108 * @param buildOption Build option, see UStringTrieBuildOption.
109 * @param errorCode Standard ICU error code. Its input value must
110 * pass the U_SUCCESS() test, or else the function returns
111 * immediately. Check for U_FAILURE() on output or use with
112 * function chaining. (See User Guide for details.)
113 * @return A StringPiece which refers to the byte-serialized BytesTrie for the add()ed data.
116 StringPiece
buildStringPiece(UStringTrieBuildOption buildOption
, UErrorCode
&errorCode
);
119 * Removes all (byte sequence, value) pairs.
120 * New data can then be add()ed and a new trie can be built.
124 BytesTrieBuilder
&clear();
127 BytesTrieBuilder(const BytesTrieBuilder
&other
); // no copy constructor
128 BytesTrieBuilder
&operator=(const BytesTrieBuilder
&other
); // no assignment operator
130 void buildBytes(UStringTrieBuildOption buildOption
, UErrorCode
&errorCode
);
132 virtual int32_t getElementStringLength(int32_t i
) const;
133 virtual char16_t getElementUnit(int32_t i
, int32_t byteIndex
) const;
134 virtual int32_t getElementValue(int32_t i
) const;
136 virtual int32_t getLimitOfLinearMatch(int32_t first
, int32_t last
, int32_t byteIndex
) const;
138 virtual int32_t countElementUnits(int32_t start
, int32_t limit
, int32_t byteIndex
) const;
139 virtual int32_t skipElementsBySomeUnits(int32_t i
, int32_t byteIndex
, int32_t count
) const;
140 virtual int32_t indexOfElementWithNextUnit(int32_t i
, int32_t byteIndex
, char16_t byte
) const;
142 virtual UBool
matchNodesCanHaveValues() const { return FALSE
; }
144 virtual int32_t getMaxBranchLinearSubNodeLength() const { return BytesTrie::kMaxBranchLinearSubNodeLength
; }
145 virtual int32_t getMinLinearMatch() const { return BytesTrie::kMinLinearMatch
; }
146 virtual int32_t getMaxLinearMatchLength() const { return BytesTrie::kMaxLinearMatchLength
; }
149 * @internal (private)
151 class BTLinearMatchNode
: public LinearMatchNode
{
153 BTLinearMatchNode(const char *units
, int32_t len
, Node
*nextNode
);
154 virtual UBool
operator==(const Node
&other
) const;
155 virtual void write(StringTrieBuilder
&builder
);
160 virtual Node
*createLinearMatchNode(int32_t i
, int32_t byteIndex
, int32_t length
,
161 Node
*nextNode
) const;
163 UBool
ensureCapacity(int32_t length
);
164 virtual int32_t write(int32_t byte
);
165 int32_t write(const char *b
, int32_t length
);
166 virtual int32_t writeElementUnits(int32_t i
, int32_t byteIndex
, int32_t length
);
167 virtual int32_t writeValueAndFinal(int32_t i
, UBool isFinal
);
168 virtual int32_t writeValueAndType(UBool hasValue
, int32_t value
, int32_t node
);
169 virtual int32_t writeDeltaTo(int32_t jumpTarget
);
171 CharString
*strings
; // Pointer not object so we need not #include internal charstr.h.
172 BytesTrieElement
*elements
;
173 int32_t elementsCapacity
;
174 int32_t elementsLength
;
176 // Byte serialization of the trie.
177 // Grows from the back: bytesLength measures from the end of the buffer!
179 int32_t bytesCapacity
;
185 #endif /* U_SHOW_CPLUSPLUS_API */
187 #endif // __BYTESTRIEBUILDER_H__