ICU 49.1.1  49.1.1
bytestriebuilder.h
1 /*
2 *******************************************************************************
3 * Copyright (C) 2010-2012, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *******************************************************************************
6 * file name: bytestriebuilder.h
7 * encoding: US-ASCII
8 * tab size: 8 (not used)
9 * indentation:4
10 *
11 * created on: 2010sep25
12 * created by: Markus W. Scherer
13 */
14 
15 #ifndef __BYTESTRIEBUILDER_H__
16 #define __BYTESTRIEBUILDER_H__
17 
18 #include "unicode/utypes.h"
19 #include "unicode/bytestrie.h"
20 #include "unicode/stringpiece.h"
21 #include "unicode/stringtriebuilder.h"
22 
24 
25 class BytesTrieElement;
26 class CharString;
27 
35 public:
41  BytesTrieBuilder(UErrorCode &errorCode);
42 
47  virtual ~BytesTrieBuilder();
48 
63  BytesTrieBuilder &add(const StringPiece &s, int32_t value, UErrorCode &errorCode);
64 
80  BytesTrie *build(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
81 
101  StringPiece buildStringPiece(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
102 
109  BytesTrieBuilder &clear();
110 
111 private:
112  BytesTrieBuilder(const BytesTrieBuilder &other); // no copy constructor
113  BytesTrieBuilder &operator=(const BytesTrieBuilder &other); // no assignment operator
114 
115  void buildBytes(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
116 
117  virtual int32_t getElementStringLength(int32_t i) const;
118  virtual UChar getElementUnit(int32_t i, int32_t byteIndex) const;
119  virtual int32_t getElementValue(int32_t i) const;
120 
121  virtual int32_t getLimitOfLinearMatch(int32_t first, int32_t last, int32_t byteIndex) const;
122 
123  virtual int32_t countElementUnits(int32_t start, int32_t limit, int32_t byteIndex) const;
124  virtual int32_t skipElementsBySomeUnits(int32_t i, int32_t byteIndex, int32_t count) const;
125  virtual int32_t indexOfElementWithNextUnit(int32_t i, int32_t byteIndex, UChar byte) const;
126 
127  virtual UBool matchNodesCanHaveValues() const { return FALSE; }
128 
129  virtual int32_t getMaxBranchLinearSubNodeLength() const { return BytesTrie::kMaxBranchLinearSubNodeLength; }
130  virtual int32_t getMinLinearMatch() const { return BytesTrie::kMinLinearMatch; }
131  virtual int32_t getMaxLinearMatchLength() const { return BytesTrie::kMaxLinearMatchLength; }
132 
133 #ifndef U_HIDE_INTERNAL_API
134 
137  class BTLinearMatchNode : public LinearMatchNode {
138  public:
139  BTLinearMatchNode(const char *units, int32_t len, Node *nextNode);
140  virtual UBool operator==(const Node &other) const;
141  virtual void write(StringTrieBuilder &builder);
142  private:
143  const char *s;
144  };
145 #endif /* U_HIDE_INTERNAL_API */
146 
147  virtual Node *createLinearMatchNode(int32_t i, int32_t byteIndex, int32_t length,
148  Node *nextNode) const;
149 
150  UBool ensureCapacity(int32_t length);
151  virtual int32_t write(int32_t byte);
152  int32_t write(const char *b, int32_t length);
153  virtual int32_t writeElementUnits(int32_t i, int32_t byteIndex, int32_t length);
154  virtual int32_t writeValueAndFinal(int32_t i, UBool isFinal);
155  virtual int32_t writeValueAndType(UBool hasValue, int32_t value, int32_t node);
156  virtual int32_t writeDeltaTo(int32_t jumpTarget);
157 
158  CharString *strings; // Pointer not object so we need not #include internal charstr.h.
159  BytesTrieElement *elements;
160  int32_t elementsCapacity;
161  int32_t elementsLength;
162 
163  // Byte serialization of the trie.
164  // Grows from the back: bytesLength measures from the end of the buffer!
165  char *bytes;
166  int32_t bytesCapacity;
167  int32_t bytesLength;
168 };
169 
171 
172 #endif // __BYTESTRIEBUILDER_H__