ICU 73.2  73.2
bytestriebuilder.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:  bytestriebuilder.h
00009 *   encoding:   UTF-8
00010 *   tab size:   8 (not used)
00011 *   indentation:4
00012 *
00013 *   created on: 2010sep25
00014 *   created by: Markus W. Scherer
00015 */
00016 
00022 #ifndef __BYTESTRIEBUILDER_H__
00023 #define __BYTESTRIEBUILDER_H__
00024 
00025 #include "unicode/utypes.h"
00026 
00027 #if U_SHOW_CPLUSPLUS_API
00028 
00029 #include "unicode/bytestrie.h"
00030 #include "unicode/stringpiece.h"
00031 #include "unicode/stringtriebuilder.h"
00032 
00033 class BytesTrieTest;
00034 
00035 U_NAMESPACE_BEGIN
00036 
00037 class BytesTrieElement;
00038 class CharString;
00045 class U_COMMON_API BytesTrieBuilder : public StringTrieBuilder {
00046 public:
00052     BytesTrieBuilder(UErrorCode &errorCode);
00053 
00058     virtual ~BytesTrieBuilder();
00059 
00074     BytesTrieBuilder &add(StringPiece s, int32_t value, UErrorCode &errorCode);
00075 
00094     BytesTrie *build(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
00095 
00119     StringPiece buildStringPiece(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
00120 
00127     BytesTrieBuilder &clear();
00128 
00129 private:
00130     friend class ::BytesTrieTest;
00131 
00132     BytesTrieBuilder(const BytesTrieBuilder &other) = delete;  // no copy constructor
00133     BytesTrieBuilder &operator=(const BytesTrieBuilder &other) = delete;  // no assignment operator
00134 
00135     void buildBytes(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
00136 
00137     virtual int32_t getElementStringLength(int32_t i) const override;
00138     virtual char16_t getElementUnit(int32_t i, int32_t byteIndex) const override;
00139     virtual int32_t getElementValue(int32_t i) const override;
00140 
00141     virtual int32_t getLimitOfLinearMatch(int32_t first, int32_t last, int32_t byteIndex) const override;
00142 
00143     virtual int32_t countElementUnits(int32_t start, int32_t limit, int32_t byteIndex) const override;
00144     virtual int32_t skipElementsBySomeUnits(int32_t i, int32_t byteIndex, int32_t count) const override;
00145     virtual int32_t indexOfElementWithNextUnit(int32_t i, int32_t byteIndex, char16_t byte) const override;
00146 
00147     virtual UBool matchNodesCanHaveValues() const override { return false; }
00148 
00149     virtual int32_t getMaxBranchLinearSubNodeLength() const override { return BytesTrie::kMaxBranchLinearSubNodeLength; }
00150     virtual int32_t getMinLinearMatch() const override { return BytesTrie::kMinLinearMatch; }
00151     virtual int32_t getMaxLinearMatchLength() const override { return BytesTrie::kMaxLinearMatchLength; }
00152 
00156     class BTLinearMatchNode : public LinearMatchNode {
00157     public:
00158         BTLinearMatchNode(const char *units, int32_t len, Node *nextNode);
00159         virtual bool operator==(const Node &other) const override;
00160         virtual void write(StringTrieBuilder &builder) override;
00161     private:
00162         const char *s;
00163     };
00164     
00165     virtual Node *createLinearMatchNode(int32_t i, int32_t byteIndex, int32_t length,
00166                                         Node *nextNode) const override;
00167 
00168     UBool ensureCapacity(int32_t length);
00169     virtual int32_t write(int32_t byte) override;
00170     int32_t write(const char *b, int32_t length);
00171     virtual int32_t writeElementUnits(int32_t i, int32_t byteIndex, int32_t length) override;
00172     virtual int32_t writeValueAndFinal(int32_t i, UBool isFinal) override;
00173     virtual int32_t writeValueAndType(UBool hasValue, int32_t value, int32_t node) override;
00174     virtual int32_t writeDeltaTo(int32_t jumpTarget) override;
00175     static int32_t internalEncodeDelta(int32_t i, char intBytes[]);
00176 
00177     CharString *strings;  // Pointer not object so we need not #include internal charstr.h.
00178     BytesTrieElement *elements;
00179     int32_t elementsCapacity;
00180     int32_t elementsLength;
00181 
00182     // Byte serialization of the trie.
00183     // Grows from the back: bytesLength measures from the end of the buffer!
00184     char *bytes;
00185     int32_t bytesCapacity;
00186     int32_t bytesLength;
00187 };
00188 
00189 U_NAMESPACE_END
00190 
00191 #endif /* U_SHOW_CPLUSPLUS_API */
00192 
00193 #endif  // __BYTESTRIEBUILDER_H__