]> git.saurik.com Git - apple/icu.git/blame_incremental - icuSources/common/unicode/bytestriebuilder.h
ICU-66108.tar.gz
[apple/icu.git] / icuSources / common / unicode / bytestriebuilder.h
... / ...
CommitLineData
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
27#if U_SHOW_CPLUSPLUS_API
28
29#include "unicode/bytestrie.h"
30#include "unicode/stringpiece.h"
31#include "unicode/stringtriebuilder.h"
32
33U_NAMESPACE_BEGIN
34
35class BytesTrieElement;
36class CharString;
37/**
38 * Builder class for BytesTrie.
39 *
40 * This class is not intended for public subclassing.
41 * @stable ICU 4.8
42 */
43class U_COMMON_API BytesTrieBuilder : public StringTrieBuilder {
44public:
45 /**
46 * Constructs an empty builder.
47 * @param errorCode Standard ICU error code.
48 * @stable ICU 4.8
49 */
50 BytesTrieBuilder(UErrorCode &errorCode);
51
52 /**
53 * Destructor.
54 * @stable ICU 4.8
55 */
56 virtual ~BytesTrieBuilder();
57
58 /**
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.)
69 * @return *this
70 * @stable ICU 4.8
71 */
72 BytesTrieBuilder &add(StringPiece s, int32_t value, UErrorCode &errorCode);
73
74 /**
75 * Builds a BytesTrie for the add()ed data.
76 * Once built, no further data can be add()ed until clear() is called.
77 *
78 * A BytesTrie cannot be empty. At least one (byte sequence, value) pair
79 * must have been add()ed.
80 *
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.
90 * @stable ICU 4.8
91 */
92 BytesTrie *build(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
93
94 /**
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.
97 *
98 * A BytesTrie cannot be empty. At least one (byte sequence, value) pair
99 * must have been add()ed.
100 *
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.
114 * @stable ICU 4.8
115 */
116 StringPiece buildStringPiece(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
117
118 /**
119 * Removes all (byte sequence, value) pairs.
120 * New data can then be add()ed and a new trie can be built.
121 * @return *this
122 * @stable ICU 4.8
123 */
124 BytesTrieBuilder &clear();
125
126private:
127 BytesTrieBuilder(const BytesTrieBuilder &other); // no copy constructor
128 BytesTrieBuilder &operator=(const BytesTrieBuilder &other); // no assignment operator
129
130 void buildBytes(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
131
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;
135
136 virtual int32_t getLimitOfLinearMatch(int32_t first, int32_t last, int32_t byteIndex) const;
137
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;
141
142 virtual UBool matchNodesCanHaveValues() const { return FALSE; }
143
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; }
147
148 /**
149 * @internal (private)
150 */
151 class BTLinearMatchNode : public LinearMatchNode {
152 public:
153 BTLinearMatchNode(const char *units, int32_t len, Node *nextNode);
154 virtual UBool operator==(const Node &other) const;
155 virtual void write(StringTrieBuilder &builder);
156 private:
157 const char *s;
158 };
159
160 virtual Node *createLinearMatchNode(int32_t i, int32_t byteIndex, int32_t length,
161 Node *nextNode) const;
162
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);
170
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;
175
176 // Byte serialization of the trie.
177 // Grows from the back: bytesLength measures from the end of the buffer!
178 char *bytes;
179 int32_t bytesCapacity;
180 int32_t bytesLength;
181};
182
183U_NAMESPACE_END
184
185#endif /* U_SHOW_CPLUSPLUS_API */
186
187#endif // __BYTESTRIEBUILDER_H__