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: ucharstriebuilder.h michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2010nov14 michael@0: * created by: Markus W. Scherer michael@0: */ michael@0: michael@0: #ifndef __UCHARSTRIEBUILDER_H__ michael@0: #define __UCHARSTRIEBUILDER_H__ michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/stringtriebuilder.h" michael@0: #include "unicode/ucharstrie.h" michael@0: #include "unicode/unistr.h" michael@0: michael@0: /** michael@0: * \file michael@0: * \brief C++ API: Builder for icu::UCharsTrie michael@0: */ michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: class UCharsTrieElement; michael@0: michael@0: /** michael@0: * Builder class for UCharsTrie. 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 UCharsTrieBuilder : 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: UCharsTrieBuilder(UErrorCode &errorCode); michael@0: michael@0: /** michael@0: * Destructor. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: virtual ~UCharsTrieBuilder(); michael@0: michael@0: /** michael@0: * Adds a (string, value) pair. michael@0: * The string must be unique. michael@0: * The string contents will be copied; the builder does not keep michael@0: * a reference to the input UnicodeString or its buffer. michael@0: * @param s The input string. michael@0: * @param value The value associated with this string. 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: UCharsTrieBuilder &add(const UnicodeString &s, int32_t value, UErrorCode &errorCode); michael@0: michael@0: /** michael@0: * Builds a UCharsTrie 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 UCharsTrie cannot be empty. At least one (string, 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 UCharsTrie for the add()ed data. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UCharsTrie *build(UStringTrieBuildOption buildOption, UErrorCode &errorCode); michael@0: michael@0: /** michael@0: * Builds a UCharsTrie for the add()ed data and UChar-serializes it. michael@0: * Once built, no further data can be add()ed until clear() is called. michael@0: * michael@0: * A UCharsTrie cannot be empty. At least one (string, value) pair michael@0: * must have been add()ed. michael@0: * michael@0: * Multiple calls to buildUnicodeString() set the UnicodeStrings to the michael@0: * builder's same UChar array, without rebuilding. michael@0: * If buildUnicodeString() is called after build(), the trie will be michael@0: * re-serialized into a new array. michael@0: * If build() is called after buildUnicodeString(), 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 result A UnicodeString which will be set to the UChar-serialized michael@0: * UCharsTrie for the add()ed data. 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 result michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UnicodeString &buildUnicodeString(UStringTrieBuildOption buildOption, UnicodeString &result, michael@0: UErrorCode &errorCode); michael@0: michael@0: /** michael@0: * Removes all (string, 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: UCharsTrieBuilder &clear() { michael@0: strings.remove(); michael@0: elementsLength=0; michael@0: ucharsLength=0; michael@0: return *this; michael@0: } michael@0: michael@0: private: michael@0: UCharsTrieBuilder(const UCharsTrieBuilder &other); // no copy constructor michael@0: UCharsTrieBuilder &operator=(const UCharsTrieBuilder &other); // no assignment operator michael@0: michael@0: void buildUChars(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 unitIndex) 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 unitIndex) const; michael@0: michael@0: virtual int32_t countElementUnits(int32_t start, int32_t limit, int32_t unitIndex) const; michael@0: virtual int32_t skipElementsBySomeUnits(int32_t i, int32_t unitIndex, int32_t count) const; michael@0: virtual int32_t indexOfElementWithNextUnit(int32_t i, int32_t unitIndex, UChar unit) const; michael@0: michael@0: virtual UBool matchNodesCanHaveValues() const { return TRUE; } michael@0: michael@0: virtual int32_t getMaxBranchLinearSubNodeLength() const { return UCharsTrie::kMaxBranchLinearSubNodeLength; } michael@0: virtual int32_t getMinLinearMatch() const { return UCharsTrie::kMinLinearMatch; } michael@0: virtual int32_t getMaxLinearMatchLength() const { return UCharsTrie::kMaxLinearMatchLength; } michael@0: michael@0: #ifndef U_HIDE_INTERNAL_API michael@0: class UCTLinearMatchNode : public LinearMatchNode { michael@0: public: michael@0: UCTLinearMatchNode(const UChar *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 UChar *s; michael@0: }; michael@0: #endif michael@0: michael@0: virtual Node *createLinearMatchNode(int32_t i, int32_t unitIndex, 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 unit); michael@0: int32_t write(const UChar *s, int32_t length); michael@0: virtual int32_t writeElementUnits(int32_t i, int32_t unitIndex, 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: UnicodeString strings; michael@0: UCharsTrieElement *elements; michael@0: int32_t elementsCapacity; michael@0: int32_t elementsLength; michael@0: michael@0: // UChar serialization of the trie. michael@0: // Grows from the back: ucharsLength measures from the end of the buffer! michael@0: UChar *uchars; michael@0: int32_t ucharsCapacity; michael@0: int32_t ucharsLength; michael@0: }; michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif // __UCHARSTRIEBUILDER_H__