ICU 49.1.1  49.1.1
bytestriebuilder.h
00001 /*
00002 *******************************************************************************
00003 *   Copyright (C) 2010-2012, International Business Machines
00004 *   Corporation and others.  All Rights Reserved.
00005 *******************************************************************************
00006 *   file name:  bytestriebuilder.h
00007 *   encoding:   US-ASCII
00008 *   tab size:   8 (not used)
00009 *   indentation:4
00010 *
00011 *   created on: 2010sep25
00012 *   created by: Markus W. Scherer
00013 */
00014 
00015 #ifndef __BYTESTRIEBUILDER_H__
00016 #define __BYTESTRIEBUILDER_H__
00017 
00018 #include "unicode/utypes.h"
00019 #include "unicode/bytestrie.h"
00020 #include "unicode/stringpiece.h"
00021 #include "unicode/stringtriebuilder.h"
00022 
00023 U_NAMESPACE_BEGIN
00024 
00025 class BytesTrieElement;
00026 class CharString;
00027 
00034 class U_COMMON_API BytesTrieBuilder : public StringTrieBuilder {
00035 public:
00041     BytesTrieBuilder(UErrorCode &errorCode);
00042 
00047     virtual ~BytesTrieBuilder();
00048 
00063     BytesTrieBuilder &add(const StringPiece &s, int32_t value, UErrorCode &errorCode);
00064 
00080     BytesTrie *build(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
00081 
00101     StringPiece buildStringPiece(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
00102 
00109     BytesTrieBuilder &clear();
00110 
00111 private:
00112     BytesTrieBuilder(const BytesTrieBuilder &other);  // no copy constructor
00113     BytesTrieBuilder &operator=(const BytesTrieBuilder &other);  // no assignment operator
00114 
00115     void buildBytes(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
00116 
00117     virtual int32_t getElementStringLength(int32_t i) const;
00118     virtual UChar getElementUnit(int32_t i, int32_t byteIndex) const;
00119     virtual int32_t getElementValue(int32_t i) const;
00120 
00121     virtual int32_t getLimitOfLinearMatch(int32_t first, int32_t last, int32_t byteIndex) const;
00122 
00123     virtual int32_t countElementUnits(int32_t start, int32_t limit, int32_t byteIndex) const;
00124     virtual int32_t skipElementsBySomeUnits(int32_t i, int32_t byteIndex, int32_t count) const;
00125     virtual int32_t indexOfElementWithNextUnit(int32_t i, int32_t byteIndex, UChar byte) const;
00126 
00127     virtual UBool matchNodesCanHaveValues() const { return FALSE; }
00128 
00129     virtual int32_t getMaxBranchLinearSubNodeLength() const { return BytesTrie::kMaxBranchLinearSubNodeLength; }
00130     virtual int32_t getMinLinearMatch() const { return BytesTrie::kMinLinearMatch; }
00131     virtual int32_t getMaxLinearMatchLength() const { return BytesTrie::kMaxLinearMatchLength; }
00132 
00133 #ifndef U_HIDE_INTERNAL_API
00134 
00137     class BTLinearMatchNode : public LinearMatchNode {
00138     public:
00139         BTLinearMatchNode(const char *units, int32_t len, Node *nextNode);
00140         virtual UBool operator==(const Node &other) const;
00141         virtual void write(StringTrieBuilder &builder);
00142     private:
00143         const char *s;
00144     };
00145 #endif  /* U_HIDE_INTERNAL_API */
00146 
00147     virtual Node *createLinearMatchNode(int32_t i, int32_t byteIndex, int32_t length,
00148                                         Node *nextNode) const;
00149 
00150     UBool ensureCapacity(int32_t length);
00151     virtual int32_t write(int32_t byte);
00152     int32_t write(const char *b, int32_t length);
00153     virtual int32_t writeElementUnits(int32_t i, int32_t byteIndex, int32_t length);
00154     virtual int32_t writeValueAndFinal(int32_t i, UBool isFinal);
00155     virtual int32_t writeValueAndType(UBool hasValue, int32_t value, int32_t node);
00156     virtual int32_t writeDeltaTo(int32_t jumpTarget);
00157 
00158     CharString *strings;  // Pointer not object so we need not #include internal charstr.h.
00159     BytesTrieElement *elements;
00160     int32_t elementsCapacity;
00161     int32_t elementsLength;
00162 
00163     // Byte serialization of the trie.
00164     // Grows from the back: bytesLength measures from the end of the buffer!
00165     char *bytes;
00166     int32_t bytesCapacity;
00167     int32_t bytesLength;
00168 };
00169 
00170 U_NAMESPACE_END
00171 
00172 #endif  // __BYTESTRIEBUILDER_H__