ICU 73.2  73.2
ucharstriebuilder.h
Go to the documentation of this file.
00001 // © 2016 and later: Unicode, Inc. and others.
00002 // License & terms of use: http://www.unicode.org/copyright.html
00003 /*
00004 *******************************************************************************
00005 *   Copyright (C) 2010-2016, International Business Machines
00006 *   Corporation and others.  All Rights Reserved.
00007 *******************************************************************************
00008 *   file name:  ucharstriebuilder.h
00009 *   encoding:   UTF-8
00010 *   tab size:   8 (not used)
00011 *   indentation:4
00012 *
00013 *   created on: 2010nov14
00014 *   created by: Markus W. Scherer
00015 */
00016 
00017 #ifndef __UCHARSTRIEBUILDER_H__
00018 #define __UCHARSTRIEBUILDER_H__
00019 
00020 #include "unicode/utypes.h"
00021 
00022 #if U_SHOW_CPLUSPLUS_API
00023 
00024 #include "unicode/stringtriebuilder.h"
00025 #include "unicode/ucharstrie.h"
00026 #include "unicode/unistr.h"
00027 
00033 U_NAMESPACE_BEGIN
00034 
00035 class UCharsTrieElement;
00036 
00043 class U_COMMON_API UCharsTrieBuilder : public StringTrieBuilder {
00044 public:
00050     UCharsTrieBuilder(UErrorCode &errorCode);
00051 
00056     virtual ~UCharsTrieBuilder();
00057 
00072     UCharsTrieBuilder &add(const UnicodeString &s, int32_t value, UErrorCode &errorCode);
00073 
00092     UCharsTrie *build(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
00093 
00119     UnicodeString &buildUnicodeString(UStringTrieBuildOption buildOption, UnicodeString &result,
00120                                       UErrorCode &errorCode);
00121 
00128     UCharsTrieBuilder &clear() {
00129         strings.remove();
00130         elementsLength=0;
00131         ucharsLength=0;
00132         return *this;
00133     }
00134 
00135 private:
00136     UCharsTrieBuilder(const UCharsTrieBuilder &other) = delete;  // no copy constructor
00137     UCharsTrieBuilder &operator=(const UCharsTrieBuilder &other) = delete;  // no assignment operator
00138 
00139     void buildUChars(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
00140 
00141     virtual int32_t getElementStringLength(int32_t i) const override;
00142     virtual char16_t getElementUnit(int32_t i, int32_t unitIndex) const override;
00143     virtual int32_t getElementValue(int32_t i) const override;
00144 
00145     virtual int32_t getLimitOfLinearMatch(int32_t first, int32_t last, int32_t unitIndex) const override;
00146 
00147     virtual int32_t countElementUnits(int32_t start, int32_t limit, int32_t unitIndex) const override;
00148     virtual int32_t skipElementsBySomeUnits(int32_t i, int32_t unitIndex, int32_t count) const override;
00149     virtual int32_t indexOfElementWithNextUnit(int32_t i, int32_t unitIndex, char16_t unit) const override;
00150 
00151     virtual UBool matchNodesCanHaveValues() const override { return true; }
00152 
00153     virtual int32_t getMaxBranchLinearSubNodeLength() const override { return UCharsTrie::kMaxBranchLinearSubNodeLength; }
00154     virtual int32_t getMinLinearMatch() const override { return UCharsTrie::kMinLinearMatch; }
00155     virtual int32_t getMaxLinearMatchLength() const override { return UCharsTrie::kMaxLinearMatchLength; }
00156 
00157     class UCTLinearMatchNode : public LinearMatchNode {
00158     public:
00159         UCTLinearMatchNode(const char16_t *units, int32_t len, Node *nextNode);
00160         virtual bool operator==(const Node &other) const override;
00161         virtual void write(StringTrieBuilder &builder) override;
00162     private:
00163         const char16_t *s;
00164     };
00165 
00166     virtual Node *createLinearMatchNode(int32_t i, int32_t unitIndex, int32_t length,
00167                                         Node *nextNode) const override;
00168 
00169     UBool ensureCapacity(int32_t length);
00170     virtual int32_t write(int32_t unit) override;
00171     int32_t write(const char16_t *s, int32_t length);
00172     virtual int32_t writeElementUnits(int32_t i, int32_t unitIndex, int32_t length) override;
00173     virtual int32_t writeValueAndFinal(int32_t i, UBool isFinal) override;
00174     virtual int32_t writeValueAndType(UBool hasValue, int32_t value, int32_t node) override;
00175     virtual int32_t writeDeltaTo(int32_t jumpTarget) override;
00176 
00177     UnicodeString strings;
00178     UCharsTrieElement *elements;
00179     int32_t elementsCapacity;
00180     int32_t elementsLength;
00181 
00182     // char16_t serialization of the trie.
00183     // Grows from the back: ucharsLength measures from the end of the buffer!
00184     char16_t *uchars;
00185     int32_t ucharsCapacity;
00186     int32_t ucharsLength;
00187 };
00188 
00189 U_NAMESPACE_END
00190 
00191 #endif /* U_SHOW_CPLUSPLUS_API */
00192 
00193 #endif  // __UCHARSTRIEBUILDER_H__