jpayne@69: // © 2016 and later: Unicode, Inc. and others. jpayne@69: // License & terms of use: http://www.unicode.org/copyright.html jpayne@69: /* jpayne@69: ******************************************************************************* jpayne@69: * Copyright (C) 2010-2016, International Business Machines jpayne@69: * Corporation and others. All Rights Reserved. jpayne@69: ******************************************************************************* jpayne@69: * file name: ucharstriebuilder.h jpayne@69: * encoding: UTF-8 jpayne@69: * tab size: 8 (not used) jpayne@69: * indentation:4 jpayne@69: * jpayne@69: * created on: 2010nov14 jpayne@69: * created by: Markus W. Scherer jpayne@69: */ jpayne@69: jpayne@69: #ifndef __UCHARSTRIEBUILDER_H__ jpayne@69: #define __UCHARSTRIEBUILDER_H__ jpayne@69: jpayne@69: #include "unicode/utypes.h" jpayne@69: jpayne@69: #if U_SHOW_CPLUSPLUS_API jpayne@69: jpayne@69: #include "unicode/stringtriebuilder.h" jpayne@69: #include "unicode/ucharstrie.h" jpayne@69: #include "unicode/unistr.h" jpayne@69: jpayne@69: /** jpayne@69: * \file jpayne@69: * \brief C++ API: Builder for icu::UCharsTrie jpayne@69: */ jpayne@69: jpayne@69: U_NAMESPACE_BEGIN jpayne@69: jpayne@69: class UCharsTrieElement; jpayne@69: jpayne@69: /** jpayne@69: * Builder class for UCharsTrie. jpayne@69: * jpayne@69: * This class is not intended for public subclassing. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: class U_COMMON_API UCharsTrieBuilder : public StringTrieBuilder { jpayne@69: public: jpayne@69: /** jpayne@69: * Constructs an empty builder. jpayne@69: * @param errorCode Standard ICU error code. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: UCharsTrieBuilder(UErrorCode &errorCode); jpayne@69: jpayne@69: /** jpayne@69: * Destructor. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: virtual ~UCharsTrieBuilder(); jpayne@69: jpayne@69: /** jpayne@69: * Adds a (string, value) pair. jpayne@69: * The string must be unique. jpayne@69: * The string contents will be copied; the builder does not keep jpayne@69: * a reference to the input UnicodeString or its buffer. jpayne@69: * @param s The input string. jpayne@69: * @param value The value associated with this string. jpayne@69: * @param errorCode Standard ICU error code. Its input value must jpayne@69: * pass the U_SUCCESS() test, or else the function returns jpayne@69: * immediately. Check for U_FAILURE() on output or use with jpayne@69: * function chaining. (See User Guide for details.) jpayne@69: * @return *this jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: UCharsTrieBuilder &add(const UnicodeString &s, int32_t value, UErrorCode &errorCode); jpayne@69: jpayne@69: /** jpayne@69: * Builds a UCharsTrie for the add()ed data. jpayne@69: * Once built, no further data can be add()ed until clear() is called. jpayne@69: * jpayne@69: * A UCharsTrie cannot be empty. At least one (string, value) pair jpayne@69: * must have been add()ed. jpayne@69: * jpayne@69: * This method passes ownership of the builder's internal result array to the new trie object. jpayne@69: * Another call to any build() variant will re-serialize the trie. jpayne@69: * After clear() has been called, a new array will be used as well. jpayne@69: * @param buildOption Build option, see UStringTrieBuildOption. jpayne@69: * @param errorCode Standard ICU error code. Its input value must jpayne@69: * pass the U_SUCCESS() test, or else the function returns jpayne@69: * immediately. Check for U_FAILURE() on output or use with jpayne@69: * function chaining. (See User Guide for details.) jpayne@69: * @return A new UCharsTrie for the add()ed data. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: UCharsTrie *build(UStringTrieBuildOption buildOption, UErrorCode &errorCode); jpayne@69: jpayne@69: /** jpayne@69: * Builds a UCharsTrie for the add()ed data and char16_t-serializes it. jpayne@69: * Once built, no further data can be add()ed until clear() is called. jpayne@69: * jpayne@69: * A UCharsTrie cannot be empty. At least one (string, value) pair jpayne@69: * must have been add()ed. jpayne@69: * jpayne@69: * Multiple calls to buildUnicodeString() set the UnicodeStrings to the jpayne@69: * builder's same char16_t array, without rebuilding. jpayne@69: * If buildUnicodeString() is called after build(), the trie will be jpayne@69: * re-serialized into a new array. jpayne@69: * If build() is called after buildUnicodeString(), the trie object will become jpayne@69: * the owner of the previously returned array. jpayne@69: * After clear() has been called, a new array will be used as well. jpayne@69: * @param buildOption Build option, see UStringTrieBuildOption. jpayne@69: * @param result A UnicodeString which will be set to the char16_t-serialized jpayne@69: * UCharsTrie for the add()ed data. jpayne@69: * @param errorCode Standard ICU error code. Its input value must jpayne@69: * pass the U_SUCCESS() test, or else the function returns jpayne@69: * immediately. Check for U_FAILURE() on output or use with jpayne@69: * function chaining. (See User Guide for details.) jpayne@69: * @return result jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: UnicodeString &buildUnicodeString(UStringTrieBuildOption buildOption, UnicodeString &result, jpayne@69: UErrorCode &errorCode); jpayne@69: jpayne@69: /** jpayne@69: * Removes all (string, value) pairs. jpayne@69: * New data can then be add()ed and a new trie can be built. jpayne@69: * @return *this jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: UCharsTrieBuilder &clear() { jpayne@69: strings.remove(); jpayne@69: elementsLength=0; jpayne@69: ucharsLength=0; jpayne@69: return *this; jpayne@69: } jpayne@69: jpayne@69: private: jpayne@69: UCharsTrieBuilder(const UCharsTrieBuilder &other); // no copy constructor jpayne@69: UCharsTrieBuilder &operator=(const UCharsTrieBuilder &other); // no assignment operator jpayne@69: jpayne@69: void buildUChars(UStringTrieBuildOption buildOption, UErrorCode &errorCode); jpayne@69: jpayne@69: virtual int32_t getElementStringLength(int32_t i) const; jpayne@69: virtual char16_t getElementUnit(int32_t i, int32_t unitIndex) const; jpayne@69: virtual int32_t getElementValue(int32_t i) const; jpayne@69: jpayne@69: virtual int32_t getLimitOfLinearMatch(int32_t first, int32_t last, int32_t unitIndex) const; jpayne@69: jpayne@69: virtual int32_t countElementUnits(int32_t start, int32_t limit, int32_t unitIndex) const; jpayne@69: virtual int32_t skipElementsBySomeUnits(int32_t i, int32_t unitIndex, int32_t count) const; jpayne@69: virtual int32_t indexOfElementWithNextUnit(int32_t i, int32_t unitIndex, char16_t unit) const; jpayne@69: jpayne@69: virtual UBool matchNodesCanHaveValues() const { return TRUE; } jpayne@69: jpayne@69: virtual int32_t getMaxBranchLinearSubNodeLength() const { return UCharsTrie::kMaxBranchLinearSubNodeLength; } jpayne@69: virtual int32_t getMinLinearMatch() const { return UCharsTrie::kMinLinearMatch; } jpayne@69: virtual int32_t getMaxLinearMatchLength() const { return UCharsTrie::kMaxLinearMatchLength; } jpayne@69: jpayne@69: class UCTLinearMatchNode : public LinearMatchNode { jpayne@69: public: jpayne@69: UCTLinearMatchNode(const char16_t *units, int32_t len, Node *nextNode); jpayne@69: virtual UBool operator==(const Node &other) const; jpayne@69: virtual void write(StringTrieBuilder &builder); jpayne@69: private: jpayne@69: const char16_t *s; jpayne@69: }; jpayne@69: jpayne@69: virtual Node *createLinearMatchNode(int32_t i, int32_t unitIndex, int32_t length, jpayne@69: Node *nextNode) const; jpayne@69: jpayne@69: UBool ensureCapacity(int32_t length); jpayne@69: virtual int32_t write(int32_t unit); jpayne@69: int32_t write(const char16_t *s, int32_t length); jpayne@69: virtual int32_t writeElementUnits(int32_t i, int32_t unitIndex, int32_t length); jpayne@69: virtual int32_t writeValueAndFinal(int32_t i, UBool isFinal); jpayne@69: virtual int32_t writeValueAndType(UBool hasValue, int32_t value, int32_t node); jpayne@69: virtual int32_t writeDeltaTo(int32_t jumpTarget); jpayne@69: jpayne@69: UnicodeString strings; jpayne@69: UCharsTrieElement *elements; jpayne@69: int32_t elementsCapacity; jpayne@69: int32_t elementsLength; jpayne@69: jpayne@69: // char16_t serialization of the trie. jpayne@69: // Grows from the back: ucharsLength measures from the end of the buffer! jpayne@69: char16_t *uchars; jpayne@69: int32_t ucharsCapacity; jpayne@69: int32_t ucharsLength; jpayne@69: }; jpayne@69: jpayne@69: U_NAMESPACE_END jpayne@69: jpayne@69: #endif /* U_SHOW_CPLUSPLUS_API */ jpayne@69: jpayne@69: #endif // __UCHARSTRIEBUILDER_H__