]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/unicode/bytestriebuilder.h
ICU-62107.0.1.tar.gz
[apple/icu.git] / icuSources / common / unicode / bytestriebuilder.h
1 // © 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: UTF-8
10 * tab size: 8 (not used)
11 * indentation:4
12 *
13 * created on: 2010sep25
14 * created by: Markus W. Scherer
15 */
16
17 /**
18 * \file
19 * \brief C++ API: Builder for icu::BytesTrie
20 */
21
22 #ifndef __BYTESTRIEBUILDER_H__
23 #define __BYTESTRIEBUILDER_H__
24
25 #include "unicode/utypes.h"
26 #include "unicode/bytestrie.h"
27 #include "unicode/stringpiece.h"
28 #include "unicode/stringtriebuilder.h"
29
30 #if U_SHOW_CPLUSPLUS_API
31 U_NAMESPACE_BEGIN
32
33 class BytesTrieElement;
34 class CharString;
35 /**
36 * Builder class for BytesTrie.
37 *
38 * This class is not intended for public subclassing.
39 * @stable ICU 4.8
40 */
41 class U_COMMON_API BytesTrieBuilder : public StringTrieBuilder {
42 public:
43 /**
44 * Constructs an empty builder.
45 * @param errorCode Standard ICU error code.
46 * @stable ICU 4.8
47 */
48 BytesTrieBuilder(UErrorCode &errorCode);
49
50 /**
51 * Destructor.
52 * @stable ICU 4.8
53 */
54 virtual ~BytesTrieBuilder();
55
56 /**
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.)
67 * @return *this
68 * @stable ICU 4.8
69 */
70 BytesTrieBuilder &add(StringPiece s, int32_t value, UErrorCode &errorCode);
71
72 /**
73 * Builds a BytesTrie for the add()ed data.
74 * Once built, no further data can be add()ed until clear() is called.
75 *
76 * A BytesTrie cannot be empty. At least one (byte sequence, value) pair
77 * must have been add()ed.
78 *
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.
88 * @stable ICU 4.8
89 */
90 BytesTrie *build(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
91
92 /**
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.
95 *
96 * A BytesTrie cannot be empty. At least one (byte sequence, value) pair
97 * must have been add()ed.
98 *
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.
112 * @stable ICU 4.8
113 */
114 StringPiece buildStringPiece(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
115
116 /**
117 * Removes all (byte sequence, value) pairs.
118 * New data can then be add()ed and a new trie can be built.
119 * @return *this
120 * @stable ICU 4.8
121 */
122 BytesTrieBuilder &clear();
123
124 private:
125 BytesTrieBuilder(const BytesTrieBuilder &other); // no copy constructor
126 BytesTrieBuilder &operator=(const BytesTrieBuilder &other); // no assignment operator
127
128 void buildBytes(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
129
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;
133
134 virtual int32_t getLimitOfLinearMatch(int32_t first, int32_t last, int32_t byteIndex) const;
135
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;
139
140 virtual UBool matchNodesCanHaveValues() const { return FALSE; }
141
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; }
145
146 /**
147 * @internal (private)
148 */
149 class BTLinearMatchNode : public LinearMatchNode {
150 public:
151 BTLinearMatchNode(const char *units, int32_t len, Node *nextNode);
152 virtual UBool operator==(const Node &other) const;
153 virtual void write(StringTrieBuilder &builder);
154 private:
155 const char *s;
156 };
157
158 virtual Node *createLinearMatchNode(int32_t i, int32_t byteIndex, int32_t length,
159 Node *nextNode) const;
160
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);
168
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;
173
174 // Byte serialization of the trie.
175 // Grows from the back: bytesLength measures from the end of the buffer!
176 char *bytes;
177 int32_t bytesCapacity;
178 int32_t bytesLength;
179 };
180
181 U_NAMESPACE_END
182 #endif // U_SHOW_CPLUSPLUS_API
183
184 #endif // __BYTESTRIEBUILDER_H__