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