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