2 *******************************************************************************
3 * Copyright (C) 2010-2012, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *******************************************************************************
6 * file name: ucharstriebuilder.h
8 * tab size: 8 (not used)
11 * created on: 2010nov14
12 * created by: Markus W. Scherer
15 #ifndef __UCHARSTRIEBUILDER_H__
16 #define __UCHARSTRIEBUILDER_H__
18 #include "unicode/utypes.h"
19 #include "unicode/stringtriebuilder.h"
20 #include "unicode/ucharstrie.h"
21 #include "unicode/unistr.h"
25 class UCharsTrieElement
;
28 * Builder class for UCharsTrie.
30 * This class is not intended for public subclassing.
33 class U_COMMON_API UCharsTrieBuilder
: public StringTrieBuilder
{
36 * Constructs an empty builder.
37 * @param errorCode Standard ICU error code.
40 UCharsTrieBuilder(UErrorCode
&errorCode
);
46 virtual ~UCharsTrieBuilder();
49 * Adds a (string, value) pair.
50 * The string must be unique.
51 * The string contents will be copied; the builder does not keep
52 * a reference to the input UnicodeString or its buffer.
53 * @param s The input string.
54 * @param value The value associated with this string.
55 * @param errorCode Standard ICU error code. Its input value must
56 * pass the U_SUCCESS() test, or else the function returns
57 * immediately. Check for U_FAILURE() on output or use with
58 * function chaining. (See User Guide for details.)
62 UCharsTrieBuilder
&add(const UnicodeString
&s
, int32_t value
, UErrorCode
&errorCode
);
65 * Builds a UCharsTrie for the add()ed data.
66 * Once built, no further data can be add()ed until clear() is called.
68 * This method passes ownership of the builder's internal result array to the new trie object.
69 * Another call to any build() variant will re-serialize the trie.
70 * After clear() has been called, a new array will be used as well.
71 * @param buildOption Build option, see UStringTrieBuildOption.
72 * @param errorCode Standard ICU error code. Its input value must
73 * pass the U_SUCCESS() test, or else the function returns
74 * immediately. Check for U_FAILURE() on output or use with
75 * function chaining. (See User Guide for details.)
76 * @return A new UCharsTrie for the add()ed data.
79 UCharsTrie
*build(UStringTrieBuildOption buildOption
, UErrorCode
&errorCode
);
82 * Builds a UCharsTrie for the add()ed data and UChar-serializes it.
83 * Once built, no further data can be add()ed until clear() is called.
85 * Multiple calls to buildUnicodeString() set the UnicodeStrings to the
86 * builder's same UChar array, without rebuilding.
87 * If buildUnicodeString() is called after build(), the trie will be
88 * re-serialized into a new array.
89 * If build() is called after buildUnicodeString(), the trie object will become
90 * the owner of the previously returned array.
91 * After clear() has been called, a new array will be used as well.
92 * @param buildOption Build option, see UStringTrieBuildOption.
93 * @param result A UnicodeString which will be set to the UChar-serialized
94 * UCharsTrie for the add()ed data.
95 * @param errorCode Standard ICU error code. Its input value must
96 * pass the U_SUCCESS() test, or else the function returns
97 * immediately. Check for U_FAILURE() on output or use with
98 * function chaining. (See User Guide for details.)
102 UnicodeString
&buildUnicodeString(UStringTrieBuildOption buildOption
, UnicodeString
&result
,
103 UErrorCode
&errorCode
);
106 * Removes all (string, value) pairs.
107 * New data can then be add()ed and a new trie can be built.
111 UCharsTrieBuilder
&clear() {
119 UCharsTrieBuilder(const UCharsTrieBuilder
&other
); // no copy constructor
120 UCharsTrieBuilder
&operator=(const UCharsTrieBuilder
&other
); // no assignment operator
122 void buildUChars(UStringTrieBuildOption buildOption
, UErrorCode
&errorCode
);
124 virtual int32_t getElementStringLength(int32_t i
) const;
125 virtual UChar
getElementUnit(int32_t i
, int32_t unitIndex
) const;
126 virtual int32_t getElementValue(int32_t i
) const;
128 virtual int32_t getLimitOfLinearMatch(int32_t first
, int32_t last
, int32_t unitIndex
) const;
130 virtual int32_t countElementUnits(int32_t start
, int32_t limit
, int32_t unitIndex
) const;
131 virtual int32_t skipElementsBySomeUnits(int32_t i
, int32_t unitIndex
, int32_t count
) const;
132 virtual int32_t indexOfElementWithNextUnit(int32_t i
, int32_t unitIndex
, UChar unit
) const;
134 virtual UBool
matchNodesCanHaveValues() const { return TRUE
; }
136 virtual int32_t getMaxBranchLinearSubNodeLength() const { return UCharsTrie::kMaxBranchLinearSubNodeLength
; }
137 virtual int32_t getMinLinearMatch() const { return UCharsTrie::kMinLinearMatch
; }
138 virtual int32_t getMaxLinearMatchLength() const { return UCharsTrie::kMaxLinearMatchLength
; }
140 #ifndef U_HIDE_INTERNAL_API
141 class UCTLinearMatchNode
: public LinearMatchNode
{
143 UCTLinearMatchNode(const UChar
*units
, int32_t len
, Node
*nextNode
);
144 virtual UBool
operator==(const Node
&other
) const;
145 virtual void write(StringTrieBuilder
&builder
);
151 virtual Node
*createLinearMatchNode(int32_t i
, int32_t unitIndex
, int32_t length
,
152 Node
*nextNode
) const;
154 UBool
ensureCapacity(int32_t length
);
155 virtual int32_t write(int32_t unit
);
156 int32_t write(const UChar
*s
, int32_t length
);
157 virtual int32_t writeElementUnits(int32_t i
, int32_t unitIndex
, int32_t length
);
158 virtual int32_t writeValueAndFinal(int32_t i
, UBool isFinal
);
159 virtual int32_t writeValueAndType(UBool hasValue
, int32_t value
, int32_t node
);
160 virtual int32_t writeDeltaTo(int32_t jumpTarget
);
162 UnicodeString strings
;
163 UCharsTrieElement
*elements
;
164 int32_t elementsCapacity
;
165 int32_t elementsLength
;
167 // UChar serialization of the trie.
168 // Grows from the back: ucharsLength measures from the end of the buffer!
170 int32_t ucharsCapacity
;
171 int32_t ucharsLength
;
176 #endif // __UCHARSTRIEBUILDER_H__