michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2010-2013, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: * file name: bytestriebuilder.h michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2010sep25 michael@0: * created by: Markus W. Scherer michael@0: */ michael@0: michael@0: /** michael@0: * \file michael@0: * \brief C++ API: Builder for icu::BytesTrie michael@0: */ michael@0: michael@0: #ifndef __BYTESTRIEBUILDER_H__ michael@0: #define __BYTESTRIEBUILDER_H__ michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/bytestrie.h" michael@0: #include "unicode/stringpiece.h" michael@0: #include "unicode/stringtriebuilder.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: class BytesTrieElement; michael@0: class CharString; michael@0: michael@0: /** michael@0: * Builder class for BytesTrie. michael@0: * michael@0: * This class is not intended for public subclassing. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: class U_COMMON_API BytesTrieBuilder : public StringTrieBuilder { michael@0: public: michael@0: /** michael@0: * Constructs an empty builder. michael@0: * @param errorCode Standard ICU error code. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: BytesTrieBuilder(UErrorCode &errorCode); michael@0: michael@0: /** michael@0: * Destructor. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: virtual ~BytesTrieBuilder(); michael@0: michael@0: /** michael@0: * Adds a (byte sequence, value) pair. michael@0: * The byte sequence must be unique. michael@0: * The bytes will be copied; the builder does not keep michael@0: * a reference to the input StringPiece or its data(). michael@0: * @param s The input byte sequence. michael@0: * @param value The value associated with this byte sequence. michael@0: * @param errorCode Standard ICU error code. Its input value must michael@0: * pass the U_SUCCESS() test, or else the function returns michael@0: * immediately. Check for U_FAILURE() on output or use with michael@0: * function chaining. (See User Guide for details.) michael@0: * @return *this michael@0: * @stable ICU 4.8 michael@0: */ michael@0: BytesTrieBuilder &add(const StringPiece &s, int32_t value, UErrorCode &errorCode); michael@0: michael@0: /** michael@0: * Builds a BytesTrie for the add()ed data. michael@0: * Once built, no further data can be add()ed until clear() is called. michael@0: * michael@0: * A BytesTrie cannot be empty. At least one (byte sequence, value) pair michael@0: * must have been add()ed. michael@0: * michael@0: * This method passes ownership of the builder's internal result array to the new trie object. michael@0: * Another call to any build() variant will re-serialize the trie. michael@0: * After clear() has been called, a new array will be used as well. michael@0: * @param buildOption Build option, see UStringTrieBuildOption. michael@0: * @param errorCode Standard ICU error code. Its input value must michael@0: * pass the U_SUCCESS() test, or else the function returns michael@0: * immediately. Check for U_FAILURE() on output or use with michael@0: * function chaining. (See User Guide for details.) michael@0: * @return A new BytesTrie for the add()ed data. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: BytesTrie *build(UStringTrieBuildOption buildOption, UErrorCode &errorCode); michael@0: michael@0: /** michael@0: * Builds a BytesTrie for the add()ed data and byte-serializes it. michael@0: * Once built, no further data can be add()ed until clear() is called. michael@0: * michael@0: * A BytesTrie cannot be empty. At least one (byte sequence, value) pair michael@0: * must have been add()ed. michael@0: * michael@0: * Multiple calls to buildStringPiece() return StringPieces referring to the michael@0: * builder's same byte array, without rebuilding. michael@0: * If buildStringPiece() is called after build(), the trie will be michael@0: * re-serialized into a new array. michael@0: * If build() is called after buildStringPiece(), the trie object will become michael@0: * the owner of the previously returned array. michael@0: * After clear() has been called, a new array will be used as well. michael@0: * @param buildOption Build option, see UStringTrieBuildOption. michael@0: * @param errorCode Standard ICU error code. Its input value must michael@0: * pass the U_SUCCESS() test, or else the function returns michael@0: * immediately. Check for U_FAILURE() on output or use with michael@0: * function chaining. (See User Guide for details.) michael@0: * @return A StringPiece which refers to the byte-serialized BytesTrie for the add()ed data. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: StringPiece buildStringPiece(UStringTrieBuildOption buildOption, UErrorCode &errorCode); michael@0: michael@0: /** michael@0: * Removes all (byte sequence, value) pairs. michael@0: * New data can then be add()ed and a new trie can be built. michael@0: * @return *this michael@0: * @stable ICU 4.8 michael@0: */ michael@0: BytesTrieBuilder &clear(); michael@0: michael@0: private: michael@0: BytesTrieBuilder(const BytesTrieBuilder &other); // no copy constructor michael@0: BytesTrieBuilder &operator=(const BytesTrieBuilder &other); // no assignment operator michael@0: michael@0: void buildBytes(UStringTrieBuildOption buildOption, UErrorCode &errorCode); michael@0: michael@0: virtual int32_t getElementStringLength(int32_t i) const; michael@0: virtual UChar getElementUnit(int32_t i, int32_t byteIndex) const; michael@0: virtual int32_t getElementValue(int32_t i) const; michael@0: michael@0: virtual int32_t getLimitOfLinearMatch(int32_t first, int32_t last, int32_t byteIndex) const; michael@0: michael@0: virtual int32_t countElementUnits(int32_t start, int32_t limit, int32_t byteIndex) const; michael@0: virtual int32_t skipElementsBySomeUnits(int32_t i, int32_t byteIndex, int32_t count) const; michael@0: virtual int32_t indexOfElementWithNextUnit(int32_t i, int32_t byteIndex, UChar byte) const; michael@0: michael@0: virtual UBool matchNodesCanHaveValues() const { return FALSE; } michael@0: michael@0: virtual int32_t getMaxBranchLinearSubNodeLength() const { return BytesTrie::kMaxBranchLinearSubNodeLength; } michael@0: virtual int32_t getMinLinearMatch() const { return BytesTrie::kMinLinearMatch; } michael@0: virtual int32_t getMaxLinearMatchLength() const { return BytesTrie::kMaxLinearMatchLength; } michael@0: michael@0: #ifndef U_HIDE_INTERNAL_API michael@0: /** michael@0: * @internal michael@0: */ michael@0: class BTLinearMatchNode : public LinearMatchNode { michael@0: public: michael@0: BTLinearMatchNode(const char *units, int32_t len, Node *nextNode); michael@0: virtual UBool operator==(const Node &other) const; michael@0: virtual void write(StringTrieBuilder &builder); michael@0: private: michael@0: const char *s; michael@0: }; michael@0: #endif /* U_HIDE_INTERNAL_API */ michael@0: michael@0: virtual Node *createLinearMatchNode(int32_t i, int32_t byteIndex, int32_t length, michael@0: Node *nextNode) const; michael@0: michael@0: UBool ensureCapacity(int32_t length); michael@0: virtual int32_t write(int32_t byte); michael@0: int32_t write(const char *b, int32_t length); michael@0: virtual int32_t writeElementUnits(int32_t i, int32_t byteIndex, int32_t length); michael@0: virtual int32_t writeValueAndFinal(int32_t i, UBool isFinal); michael@0: virtual int32_t writeValueAndType(UBool hasValue, int32_t value, int32_t node); michael@0: virtual int32_t writeDeltaTo(int32_t jumpTarget); michael@0: michael@0: CharString *strings; // Pointer not object so we need not #include internal charstr.h. michael@0: BytesTrieElement *elements; michael@0: int32_t elementsCapacity; michael@0: int32_t elementsLength; michael@0: michael@0: // Byte serialization of the trie. michael@0: // Grows from the back: bytesLength measures from the end of the buffer! michael@0: char *bytes; michael@0: int32_t bytesCapacity; michael@0: int32_t bytesLength; michael@0: }; michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif // __BYTESTRIEBUILDER_H__