• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:  ucharstriebuilder.h
9 *   encoding:   UTF-8
10 *   tab size:   8 (not used)
11 *   indentation:4
12 *
13 *   created on: 2010nov14
14 *   created by: Markus W. Scherer
15 */
16 
17 #ifndef __UCHARSTRIEBUILDER_H__
18 #define __UCHARSTRIEBUILDER_H__
19 
20 #include "unicode/utypes.h"
21 #include "unicode/stringtriebuilder.h"
22 #include "unicode/ucharstrie.h"
23 #include "unicode/unistr.h"
24 
25 /**
26  * \file
27  * \brief C++ API: Builder for icu::UCharsTrie
28  */
29 
30 U_NAMESPACE_BEGIN
31 
32 class UCharsTrieElement;
33 
34 /**
35  * Builder class for UCharsTrie.
36  *
37  * This class is not intended for public subclassing.
38  * @stable ICU 4.8
39  */
40 class U_COMMON_API UCharsTrieBuilder : public StringTrieBuilder {
41 public:
42     /**
43      * Constructs an empty builder.
44      * @param errorCode Standard ICU error code.
45      * @stable ICU 4.8
46      */
47     UCharsTrieBuilder(UErrorCode &errorCode);
48 
49     /**
50      * Destructor.
51      * @stable ICU 4.8
52      */
53     virtual ~UCharsTrieBuilder();
54 
55     /**
56      * Adds a (string, value) pair.
57      * The string must be unique.
58      * The string contents will be copied; the builder does not keep
59      * a reference to the input UnicodeString or its buffer.
60      * @param s The input string.
61      * @param value The value associated with this string.
62      * @param errorCode Standard ICU error code. Its input value must
63      *                  pass the U_SUCCESS() test, or else the function returns
64      *                  immediately. Check for U_FAILURE() on output or use with
65      *                  function chaining. (See User Guide for details.)
66      * @return *this
67      * @stable ICU 4.8
68      */
69     UCharsTrieBuilder &add(const UnicodeString &s, int32_t value, UErrorCode &errorCode);
70 
71     /**
72      * Builds a UCharsTrie for the add()ed data.
73      * Once built, no further data can be add()ed until clear() is called.
74      *
75      * A UCharsTrie cannot be empty. At least one (string, value) pair
76      * must have been add()ed.
77      *
78      * This method passes ownership of the builder's internal result array to the new trie object.
79      * Another call to any build() variant will re-serialize the trie.
80      * After clear() has been called, a new array will be used as well.
81      * @param buildOption Build option, see UStringTrieBuildOption.
82      * @param errorCode Standard ICU error code. Its input value must
83      *                  pass the U_SUCCESS() test, or else the function returns
84      *                  immediately. Check for U_FAILURE() on output or use with
85      *                  function chaining. (See User Guide for details.)
86      * @return A new UCharsTrie for the add()ed data.
87      * @stable ICU 4.8
88      */
89     UCharsTrie *build(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
90 
91     /**
92      * Builds a UCharsTrie for the add()ed data and char16_t-serializes it.
93      * Once built, no further data can be add()ed until clear() is called.
94      *
95      * A UCharsTrie cannot be empty. At least one (string, value) pair
96      * must have been add()ed.
97      *
98      * Multiple calls to buildUnicodeString() set the UnicodeStrings to the
99      * builder's same char16_t array, without rebuilding.
100      * If buildUnicodeString() is called after build(), the trie will be
101      * re-serialized into a new array.
102      * If build() is called after buildUnicodeString(), the trie object will become
103      * the owner of the previously returned array.
104      * After clear() has been called, a new array will be used as well.
105      * @param buildOption Build option, see UStringTrieBuildOption.
106      * @param result A UnicodeString which will be set to the char16_t-serialized
107      *               UCharsTrie for the add()ed data.
108      * @param errorCode Standard ICU error code. Its input value must
109      *                  pass the U_SUCCESS() test, or else the function returns
110      *                  immediately. Check for U_FAILURE() on output or use with
111      *                  function chaining. (See User Guide for details.)
112      * @return result
113      * @stable ICU 4.8
114      */
115     UnicodeString &buildUnicodeString(UStringTrieBuildOption buildOption, UnicodeString &result,
116                                       UErrorCode &errorCode);
117 
118     /**
119      * Removes all (string, 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      */
clear()124     UCharsTrieBuilder &clear() {
125         strings.remove();
126         elementsLength=0;
127         ucharsLength=0;
128         return *this;
129     }
130 
131 private:
132     UCharsTrieBuilder(const UCharsTrieBuilder &other);  // no copy constructor
133     UCharsTrieBuilder &operator=(const UCharsTrieBuilder &other);  // no assignment operator
134 
135     void buildUChars(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
136 
137     virtual int32_t getElementStringLength(int32_t i) const;
138     virtual char16_t getElementUnit(int32_t i, int32_t unitIndex) const;
139     virtual int32_t getElementValue(int32_t i) const;
140 
141     virtual int32_t getLimitOfLinearMatch(int32_t first, int32_t last, int32_t unitIndex) const;
142 
143     virtual int32_t countElementUnits(int32_t start, int32_t limit, int32_t unitIndex) const;
144     virtual int32_t skipElementsBySomeUnits(int32_t i, int32_t unitIndex, int32_t count) const;
145     virtual int32_t indexOfElementWithNextUnit(int32_t i, int32_t unitIndex, char16_t unit) const;
146 
matchNodesCanHaveValues()147     virtual UBool matchNodesCanHaveValues() const { return TRUE; }
148 
getMaxBranchLinearSubNodeLength()149     virtual int32_t getMaxBranchLinearSubNodeLength() const { return UCharsTrie::kMaxBranchLinearSubNodeLength; }
getMinLinearMatch()150     virtual int32_t getMinLinearMatch() const { return UCharsTrie::kMinLinearMatch; }
getMaxLinearMatchLength()151     virtual int32_t getMaxLinearMatchLength() const { return UCharsTrie::kMaxLinearMatchLength; }
152 
153     class UCTLinearMatchNode : public LinearMatchNode {
154     public:
155         UCTLinearMatchNode(const char16_t *units, int32_t len, Node *nextNode);
156         virtual UBool operator==(const Node &other) const;
157         virtual void write(StringTrieBuilder &builder);
158     private:
159         const char16_t *s;
160     };
161 
162     virtual Node *createLinearMatchNode(int32_t i, int32_t unitIndex, int32_t length,
163                                         Node *nextNode) const;
164 
165     UBool ensureCapacity(int32_t length);
166     virtual int32_t write(int32_t unit);
167     int32_t write(const char16_t *s, int32_t length);
168     virtual int32_t writeElementUnits(int32_t i, int32_t unitIndex, int32_t length);
169     virtual int32_t writeValueAndFinal(int32_t i, UBool isFinal);
170     virtual int32_t writeValueAndType(UBool hasValue, int32_t value, int32_t node);
171     virtual int32_t writeDeltaTo(int32_t jumpTarget);
172 
173     UnicodeString strings;
174     UCharsTrieElement *elements;
175     int32_t elementsCapacity;
176     int32_t elementsLength;
177 
178     // char16_t serialization of the trie.
179     // Grows from the back: ucharsLength measures from the end of the buffer!
180     char16_t *uchars;
181     int32_t ucharsCapacity;
182     int32_t ucharsLength;
183 };
184 
185 U_NAMESPACE_END
186 
187 #endif  // __UCHARSTRIEBUILDER_H__
188