ICU 49.1.1  49.1.1
ucharstriebuilder.h
1 /*
2 *******************************************************************************
3 * Copyright (C) 2010-2012, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *******************************************************************************
6 * file name: ucharstriebuilder.h
7 * encoding: US-ASCII
8 * tab size: 8 (not used)
9 * indentation:4
10 *
11 * created on: 2010nov14
12 * created by: Markus W. Scherer
13 */
14 
15 #ifndef __UCHARSTRIEBUILDER_H__
16 #define __UCHARSTRIEBUILDER_H__
17 
18 #include "unicode/utypes.h"
19 #include "unicode/stringtriebuilder.h"
20 #include "unicode/ucharstrie.h"
21 #include "unicode/unistr.h"
22 
24 
25 class UCharsTrieElement;
26 
34 public:
40  UCharsTrieBuilder(UErrorCode &errorCode);
41 
46  virtual ~UCharsTrieBuilder();
47 
62  UCharsTrieBuilder &add(const UnicodeString &s, int32_t value, UErrorCode &errorCode);
63 
79  UCharsTrie *build(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
80 
102  UnicodeString &buildUnicodeString(UStringTrieBuildOption buildOption, UnicodeString &result,
103  UErrorCode &errorCode);
104 
112  strings.remove();
113  elementsLength=0;
114  ucharsLength=0;
115  return *this;
116  }
117 
118 private:
119  UCharsTrieBuilder(const UCharsTrieBuilder &other); // no copy constructor
120  UCharsTrieBuilder &operator=(const UCharsTrieBuilder &other); // no assignment operator
121 
122  void buildUChars(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
123 
124  virtual int32_t getElementStringLength(int32_t i) const;
125  virtual UChar getElementUnit(int32_t i, int32_t unitIndex) const;
126  virtual int32_t getElementValue(int32_t i) const;
127 
128  virtual int32_t getLimitOfLinearMatch(int32_t first, int32_t last, int32_t unitIndex) const;
129 
130  virtual int32_t countElementUnits(int32_t start, int32_t limit, int32_t unitIndex) const;
131  virtual int32_t skipElementsBySomeUnits(int32_t i, int32_t unitIndex, int32_t count) const;
132  virtual int32_t indexOfElementWithNextUnit(int32_t i, int32_t unitIndex, UChar unit) const;
133 
134  virtual UBool matchNodesCanHaveValues() const { return TRUE; }
135 
136  virtual int32_t getMaxBranchLinearSubNodeLength() const { return UCharsTrie::kMaxBranchLinearSubNodeLength; }
137  virtual int32_t getMinLinearMatch() const { return UCharsTrie::kMinLinearMatch; }
138  virtual int32_t getMaxLinearMatchLength() const { return UCharsTrie::kMaxLinearMatchLength; }
139 
140 #ifndef U_HIDE_INTERNAL_API
141  class UCTLinearMatchNode : public LinearMatchNode {
142  public:
143  UCTLinearMatchNode(const UChar *units, int32_t len, Node *nextNode);
144  virtual UBool operator==(const Node &other) const;
145  virtual void write(StringTrieBuilder &builder);
146  private:
147  const UChar *s;
148  };
149 #endif
150 
151  virtual Node *createLinearMatchNode(int32_t i, int32_t unitIndex, int32_t length,
152  Node *nextNode) const;
153 
154  UBool ensureCapacity(int32_t length);
155  virtual int32_t write(int32_t unit);
156  int32_t write(const UChar *s, int32_t length);
157  virtual int32_t writeElementUnits(int32_t i, int32_t unitIndex, int32_t length);
158  virtual int32_t writeValueAndFinal(int32_t i, UBool isFinal);
159  virtual int32_t writeValueAndType(UBool hasValue, int32_t value, int32_t node);
160  virtual int32_t writeDeltaTo(int32_t jumpTarget);
161 
162  UnicodeString strings;
163  UCharsTrieElement *elements;
164  int32_t elementsCapacity;
165  int32_t elementsLength;
166 
167  // UChar serialization of the trie.
168  // Grows from the back: ucharsLength measures from the end of the buffer!
169  UChar *uchars;
170  int32_t ucharsCapacity;
171  int32_t ucharsLength;
172 };
173 
175 
176 #endif // __UCHARSTRIEBUILDER_H__