1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/common/unicode/ucharstriebuilder.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,187 @@ 1.4 +/* 1.5 +******************************************************************************* 1.6 +* Copyright (C) 2010-2013, International Business Machines 1.7 +* Corporation and others. All Rights Reserved. 1.8 +******************************************************************************* 1.9 +* file name: ucharstriebuilder.h 1.10 +* encoding: US-ASCII 1.11 +* tab size: 8 (not used) 1.12 +* indentation:4 1.13 +* 1.14 +* created on: 2010nov14 1.15 +* created by: Markus W. Scherer 1.16 +*/ 1.17 + 1.18 +#ifndef __UCHARSTRIEBUILDER_H__ 1.19 +#define __UCHARSTRIEBUILDER_H__ 1.20 + 1.21 +#include "unicode/utypes.h" 1.22 +#include "unicode/stringtriebuilder.h" 1.23 +#include "unicode/ucharstrie.h" 1.24 +#include "unicode/unistr.h" 1.25 + 1.26 +/** 1.27 + * \file 1.28 + * \brief C++ API: Builder for icu::UCharsTrie 1.29 + */ 1.30 + 1.31 +U_NAMESPACE_BEGIN 1.32 + 1.33 +class UCharsTrieElement; 1.34 + 1.35 +/** 1.36 + * Builder class for UCharsTrie. 1.37 + * 1.38 + * This class is not intended for public subclassing. 1.39 + * @stable ICU 4.8 1.40 + */ 1.41 +class U_COMMON_API UCharsTrieBuilder : public StringTrieBuilder { 1.42 +public: 1.43 + /** 1.44 + * Constructs an empty builder. 1.45 + * @param errorCode Standard ICU error code. 1.46 + * @stable ICU 4.8 1.47 + */ 1.48 + UCharsTrieBuilder(UErrorCode &errorCode); 1.49 + 1.50 + /** 1.51 + * Destructor. 1.52 + * @stable ICU 4.8 1.53 + */ 1.54 + virtual ~UCharsTrieBuilder(); 1.55 + 1.56 + /** 1.57 + * Adds a (string, value) pair. 1.58 + * The string must be unique. 1.59 + * The string contents will be copied; the builder does not keep 1.60 + * a reference to the input UnicodeString or its buffer. 1.61 + * @param s The input string. 1.62 + * @param value The value associated with this string. 1.63 + * @param errorCode Standard ICU error code. Its input value must 1.64 + * pass the U_SUCCESS() test, or else the function returns 1.65 + * immediately. Check for U_FAILURE() on output or use with 1.66 + * function chaining. (See User Guide for details.) 1.67 + * @return *this 1.68 + * @stable ICU 4.8 1.69 + */ 1.70 + UCharsTrieBuilder &add(const UnicodeString &s, int32_t value, UErrorCode &errorCode); 1.71 + 1.72 + /** 1.73 + * Builds a UCharsTrie for the add()ed data. 1.74 + * Once built, no further data can be add()ed until clear() is called. 1.75 + * 1.76 + * A UCharsTrie cannot be empty. At least one (string, value) pair 1.77 + * must have been add()ed. 1.78 + * 1.79 + * This method passes ownership of the builder's internal result array to the new trie object. 1.80 + * Another call to any build() variant will re-serialize the trie. 1.81 + * After clear() has been called, a new array will be used as well. 1.82 + * @param buildOption Build option, see UStringTrieBuildOption. 1.83 + * @param errorCode Standard ICU error code. Its input value must 1.84 + * pass the U_SUCCESS() test, or else the function returns 1.85 + * immediately. Check for U_FAILURE() on output or use with 1.86 + * function chaining. (See User Guide for details.) 1.87 + * @return A new UCharsTrie for the add()ed data. 1.88 + * @stable ICU 4.8 1.89 + */ 1.90 + UCharsTrie *build(UStringTrieBuildOption buildOption, UErrorCode &errorCode); 1.91 + 1.92 + /** 1.93 + * Builds a UCharsTrie for the add()ed data and UChar-serializes it. 1.94 + * Once built, no further data can be add()ed until clear() is called. 1.95 + * 1.96 + * A UCharsTrie cannot be empty. At least one (string, value) pair 1.97 + * must have been add()ed. 1.98 + * 1.99 + * Multiple calls to buildUnicodeString() set the UnicodeStrings to the 1.100 + * builder's same UChar array, without rebuilding. 1.101 + * If buildUnicodeString() is called after build(), the trie will be 1.102 + * re-serialized into a new array. 1.103 + * If build() is called after buildUnicodeString(), the trie object will become 1.104 + * the owner of the previously returned array. 1.105 + * After clear() has been called, a new array will be used as well. 1.106 + * @param buildOption Build option, see UStringTrieBuildOption. 1.107 + * @param result A UnicodeString which will be set to the UChar-serialized 1.108 + * UCharsTrie for the add()ed data. 1.109 + * @param errorCode Standard ICU error code. Its input value must 1.110 + * pass the U_SUCCESS() test, or else the function returns 1.111 + * immediately. Check for U_FAILURE() on output or use with 1.112 + * function chaining. (See User Guide for details.) 1.113 + * @return result 1.114 + * @stable ICU 4.8 1.115 + */ 1.116 + UnicodeString &buildUnicodeString(UStringTrieBuildOption buildOption, UnicodeString &result, 1.117 + UErrorCode &errorCode); 1.118 + 1.119 + /** 1.120 + * Removes all (string, value) pairs. 1.121 + * New data can then be add()ed and a new trie can be built. 1.122 + * @return *this 1.123 + * @stable ICU 4.8 1.124 + */ 1.125 + UCharsTrieBuilder &clear() { 1.126 + strings.remove(); 1.127 + elementsLength=0; 1.128 + ucharsLength=0; 1.129 + return *this; 1.130 + } 1.131 + 1.132 +private: 1.133 + UCharsTrieBuilder(const UCharsTrieBuilder &other); // no copy constructor 1.134 + UCharsTrieBuilder &operator=(const UCharsTrieBuilder &other); // no assignment operator 1.135 + 1.136 + void buildUChars(UStringTrieBuildOption buildOption, UErrorCode &errorCode); 1.137 + 1.138 + virtual int32_t getElementStringLength(int32_t i) const; 1.139 + virtual UChar getElementUnit(int32_t i, int32_t unitIndex) const; 1.140 + virtual int32_t getElementValue(int32_t i) const; 1.141 + 1.142 + virtual int32_t getLimitOfLinearMatch(int32_t first, int32_t last, int32_t unitIndex) const; 1.143 + 1.144 + virtual int32_t countElementUnits(int32_t start, int32_t limit, int32_t unitIndex) const; 1.145 + virtual int32_t skipElementsBySomeUnits(int32_t i, int32_t unitIndex, int32_t count) const; 1.146 + virtual int32_t indexOfElementWithNextUnit(int32_t i, int32_t unitIndex, UChar unit) const; 1.147 + 1.148 + virtual UBool matchNodesCanHaveValues() const { return TRUE; } 1.149 + 1.150 + virtual int32_t getMaxBranchLinearSubNodeLength() const { return UCharsTrie::kMaxBranchLinearSubNodeLength; } 1.151 + virtual int32_t getMinLinearMatch() const { return UCharsTrie::kMinLinearMatch; } 1.152 + virtual int32_t getMaxLinearMatchLength() const { return UCharsTrie::kMaxLinearMatchLength; } 1.153 + 1.154 +#ifndef U_HIDE_INTERNAL_API 1.155 + class UCTLinearMatchNode : public LinearMatchNode { 1.156 + public: 1.157 + UCTLinearMatchNode(const UChar *units, int32_t len, Node *nextNode); 1.158 + virtual UBool operator==(const Node &other) const; 1.159 + virtual void write(StringTrieBuilder &builder); 1.160 + private: 1.161 + const UChar *s; 1.162 + }; 1.163 +#endif 1.164 + 1.165 + virtual Node *createLinearMatchNode(int32_t i, int32_t unitIndex, int32_t length, 1.166 + Node *nextNode) const; 1.167 + 1.168 + UBool ensureCapacity(int32_t length); 1.169 + virtual int32_t write(int32_t unit); 1.170 + int32_t write(const UChar *s, int32_t length); 1.171 + virtual int32_t writeElementUnits(int32_t i, int32_t unitIndex, int32_t length); 1.172 + virtual int32_t writeValueAndFinal(int32_t i, UBool isFinal); 1.173 + virtual int32_t writeValueAndType(UBool hasValue, int32_t value, int32_t node); 1.174 + virtual int32_t writeDeltaTo(int32_t jumpTarget); 1.175 + 1.176 + UnicodeString strings; 1.177 + UCharsTrieElement *elements; 1.178 + int32_t elementsCapacity; 1.179 + int32_t elementsLength; 1.180 + 1.181 + // UChar serialization of the trie. 1.182 + // Grows from the back: ucharsLength measures from the end of the buffer! 1.183 + UChar *uchars; 1.184 + int32_t ucharsCapacity; 1.185 + int32_t ucharsLength; 1.186 +}; 1.187 + 1.188 +U_NAMESPACE_END 1.189 + 1.190 +#endif // __UCHARSTRIEBUILDER_H__