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