1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
* Copyright (C) 2010-2012, International Business Machines
* Corporation and others. All Rights Reserved.
*******************************************************************************
* file name: ucharstriebuilder.h
* encoding: UTF-8
* tab size: 8 (not used)
* indentation:4
*
* created on: 2010nov14
* created by: Markus W. Scherer
*/
#include "unicode/utypes.h"
#include "unicode/ucharstrie.h"
#include "unicode/ucharstriebuilder.h"
#include "unicode/unistr.h"
#include "unicode/ustring.h"
#include "cmemory.h"
#include "uarrsort.h"
#include "uassert.h"
#include "uhash.h"
#include "ustr_imp.h"
U_NAMESPACE_BEGIN
/*
* Note: This builder implementation stores (string, value) pairs with full copies
* of the 16-bit-unit sequences, until the UCharsTrie is built.
* It might(!) take less memory if we collected the data in a temporary, dynamic trie.
*/
class UCharsTrieElement : public UMemory {
public:
// Use compiler's default constructor, initializes nothing.
void setTo(const UnicodeString &s, int32_t val, UnicodeString &strings, UErrorCode &errorCode);
UnicodeString getString(const UnicodeString &strings) const {
int32_t length=strings[stringOffset];
return strings.tempSubString(stringOffset+1, length);
}
int32_t getStringLength(const UnicodeString &strings) const {
return strings[stringOffset];
}
char16_t charAt(int32_t index, const UnicodeString &strings) const {
return strings[stringOffset+1+index];
}
int32_t getValue() const { return value; }
int32_t compareStringTo(const UCharsTrieElement &o, const UnicodeString &strings) const;
private:
// The first strings unit contains the string length.
// (Compared with a stringLength field here, this saves 2 bytes per string.)
int32_t stringOffset;
int32_t value;
};
void
UCharsTrieElement::setTo(const UnicodeString &s, int32_t val,
UnicodeString &strings, UErrorCode &errorCode) {
if(U_FAILURE(errorCode)) {
return;
}
int32_t length=s.length();
if(length>0xffff) {
// Too long: We store the length in 1 unit.
errorCode=U_INDEX_OUTOFBOUNDS_ERROR;
return;
}
stringOffset=strings.length();
strings.append((char16_t)length);
value=val;
strings.append(s);
}
int32_t
UCharsTrieElement::compareStringTo(const UCharsTrieElement &other, const UnicodeString &strings) const {
return getString(strings).compare(other.getString(strings));
}
UCharsTrieBuilder::UCharsTrieBuilder(UErrorCode & /*errorCode*/)
: elements(nullptr), elementsCapacity(0), elementsLength(0),
uchars(nullptr), ucharsCapacity(0), ucharsLength(0) {}
UCharsTrieBuilder::~UCharsTrieBuilder() {
delete[] elements;
uprv_free(uchars);
}
UCharsTrieBuilder &
UCharsTrieBuilder::add(const UnicodeString &s, int32_t value, UErrorCode &errorCode) {
if(U_FAILURE(errorCode)) {
return *this;
}
if(ucharsLength>0) {
// Cannot add elements after building.
errorCode=U_NO_WRITE_PERMISSION;
return *this;
}
if(elementsLength==elementsCapacity) {
int32_t newCapacity;
if(elementsCapacity==0) {
newCapacity=1024;
} else {
newCapacity=4*elementsCapacity;
}
UCharsTrieElement *newElements=new UCharsTrieElement[newCapacity];
if(newElements==nullptr) {
errorCode=U_MEMORY_ALLOCATION_ERROR;
return *this;
}
if(elementsLength>0) {
uprv_memcpy(newElements, elements, (size_t)elementsLength*sizeof(UCharsTrieElement));
}
delete[] elements;
elements=newElements;
elementsCapacity=newCapacity;
}
elements[elementsLength++].setTo(s, value, strings, errorCode);
if(U_SUCCESS(errorCode) && strings.isBogus()) {
errorCode=U_MEMORY_ALLOCATION_ERROR;
}
return *this;
}
U_CDECL_BEGIN
static int32_t U_CALLCONV
compareElementStrings(const void *context, const void *left, const void *right) {
const UnicodeString *strings=static_cast<const UnicodeString *>(context);
const UCharsTrieElement *leftElement=static_cast<const UCharsTrieElement *>(left);
const UCharsTrieElement *rightElement=static_cast<const UCharsTrieElement *>(right);
return leftElement->compareStringTo(*rightElement, *strings);
}
U_CDECL_END
UCharsTrie *
UCharsTrieBuilder::build(UStringTrieBuildOption buildOption, UErrorCode &errorCode) {
buildUChars(buildOption, errorCode);
UCharsTrie *newTrie=nullptr;
if(U_SUCCESS(errorCode)) {
newTrie=new UCharsTrie(uchars, uchars+(ucharsCapacity-ucharsLength));
if(newTrie==nullptr) {
errorCode=U_MEMORY_ALLOCATION_ERROR;
} else {
uchars=nullptr; // The new trie now owns the array.
ucharsCapacity=0;
}
}
return newTrie;
}
UnicodeString &
UCharsTrieBuilder::buildUnicodeString(UStringTrieBuildOption buildOption, UnicodeString &result,
UErrorCode &errorCode) {
buildUChars(buildOption, errorCode);
if(U_SUCCESS(errorCode)) {
result.setTo(false, uchars+(ucharsCapacity-ucharsLength), ucharsLength);
}
return result;
}
void
UCharsTrieBuilder::buildUChars(UStringTrieBuildOption buildOption, UErrorCode &errorCode) {
if(U_FAILURE(errorCode)) {
return;
}
if(uchars!=nullptr && ucharsLength>0) {
// Already built.
return;
}
if(ucharsLength==0) {
if(elementsLength==0) {
errorCode=U_INDEX_OUTOFBOUNDS_ERROR;
return;
}
if(strings.isBogus()) {
errorCode=U_MEMORY_ALLOCATION_ERROR;
return;
}
uprv_sortArray(elements, elementsLength, (int32_t)sizeof(UCharsTrieElement),
compareElementStrings, &strings,
false, // need not be a stable sort
&errorCode);
if(U_FAILURE(errorCode)) {
return;
}
// Duplicate strings are not allowed.
UnicodeString prev=elements[0].getString(strings);
for(int32_t i=1; i<elementsLength; ++i) {
UnicodeString current=elements[i].getString(strings);
if(prev==current) {
errorCode=U_ILLEGAL_ARGUMENT_ERROR;
return;
}
prev.fastCopyFrom(current);
}
}
// Create and char16_t-serialize the trie for the elements.
ucharsLength=0;
int32_t capacity=strings.length();
if(capacity<1024) {
capacity=1024;
}
if(ucharsCapacity<capacity) {
uprv_free(uchars);
uchars=static_cast<char16_t *>(uprv_malloc(capacity*2));
if(uchars==nullptr) {
errorCode=U_MEMORY_ALLOCATION_ERROR;
ucharsCapacity=0;
return;
}
ucharsCapacity=capacity;
}
StringTrieBuilder::build(buildOption, elementsLength, errorCode);
if(uchars==nullptr) {
errorCode=U_MEMORY_ALLOCATION_ERROR;
}
}
int32_t
UCharsTrieBuilder::getElementStringLength(int32_t i) const {
return elements[i].getStringLength(strings);
}
char16_t
UCharsTrieBuilder::getElementUnit(int32_t i, int32_t unitIndex) const {
return elements[i].charAt(unitIndex, strings);
}
int32_t
UCharsTrieBuilder::getElementValue(int32_t i) const {
return elements[i].getValue();
}
int32_t
UCharsTrieBuilder::getLimitOfLinearMatch(int32_t first, int32_t last, int32_t unitIndex) const {
const UCharsTrieElement &firstElement=elements[first];
const UCharsTrieElement &lastElement=elements[last];
int32_t minStringLength=firstElement.getStringLength(strings);
while(++unitIndex<minStringLength &&
firstElement.charAt(unitIndex, strings)==
lastElement.charAt(unitIndex, strings)) {}
return unitIndex;
}
int32_t
UCharsTrieBuilder::countElementUnits(int32_t start, int32_t limit, int32_t unitIndex) const {
int32_t length=0; // Number of different units at unitIndex.
int32_t i=start;
do {
char16_t unit=elements[i++].charAt(unitIndex, strings);
while(i<limit && unit==elements[i].charAt(unitIndex, strings)) {
++i;
}
++length;
} while(i<limit);
return length;
}
int32_t
UCharsTrieBuilder::skipElementsBySomeUnits(int32_t i, int32_t unitIndex, int32_t count) const {
do {
char16_t unit=elements[i++].charAt(unitIndex, strings);
while(unit==elements[i].charAt(unitIndex, strings)) {
++i;
}
} while(--count>0);
return i;
}
int32_t
UCharsTrieBuilder::indexOfElementWithNextUnit(int32_t i, int32_t unitIndex, char16_t unit) const {
while(unit==elements[i].charAt(unitIndex, strings)) {
++i;
}
return i;
}
UCharsTrieBuilder::UCTLinearMatchNode::UCTLinearMatchNode(const char16_t *units, int32_t len, Node *nextNode)
: LinearMatchNode(len, nextNode), s(units) {
hash=hash*37u+ustr_hashUCharsN(units, len);
}
bool
UCharsTrieBuilder::UCTLinearMatchNode::operator==(const Node &other) const {
if(this==&other) {
return true;
}
if(!LinearMatchNode::operator==(other)) {
return false;
}
const UCTLinearMatchNode &o=static_cast<const UCTLinearMatchNode &>(other);
return 0==u_memcmp(s, o.s, length);
}
void
UCharsTrieBuilder::UCTLinearMatchNode::write(StringTrieBuilder &builder) {
UCharsTrieBuilder &b=(UCharsTrieBuilder &)builder;
next->write(builder);
b.write(s, length);
offset=b.writeValueAndType(hasValue, value, b.getMinLinearMatch()+length-1);
}
StringTrieBuilder::Node *
UCharsTrieBuilder::createLinearMatchNode(int32_t i, int32_t unitIndex, int32_t length,
Node *nextNode) const {
return new UCTLinearMatchNode(
elements[i].getString(strings).getBuffer()+unitIndex,
length,
nextNode);
}
UBool
UCharsTrieBuilder::ensureCapacity(int32_t length) {
if(uchars==nullptr) {
return false; // previous memory allocation had failed
}
if(length>ucharsCapacity) {
int32_t newCapacity=ucharsCapacity;
do {
newCapacity*=2;
} while(newCapacity<=length);
char16_t *newUChars=static_cast<char16_t *>(uprv_malloc(newCapacity*2));
if(newUChars==nullptr) {
// unable to allocate memory
uprv_free(uchars);
uchars=nullptr;
ucharsCapacity=0;
return false;
}
u_memcpy(newUChars+(newCapacity-ucharsLength),
uchars+(ucharsCapacity-ucharsLength), ucharsLength);
uprv_free(uchars);
uchars=newUChars;
ucharsCapacity=newCapacity;
}
return true;
}
int32_t
UCharsTrieBuilder::write(int32_t unit) {
int32_t newLength=ucharsLength+1;
if(ensureCapacity(newLength)) {
ucharsLength=newLength;
uchars[ucharsCapacity-ucharsLength]=(char16_t)unit;
}
return ucharsLength;
}
int32_t
UCharsTrieBuilder::write(const char16_t *s, int32_t length) {
int32_t newLength=ucharsLength+length;
if(ensureCapacity(newLength)) {
ucharsLength=newLength;
u_memcpy(uchars+(ucharsCapacity-ucharsLength), s, length);
}
return ucharsLength;
}
int32_t
UCharsTrieBuilder::writeElementUnits(int32_t i, int32_t unitIndex, int32_t length) {
return write(elements[i].getString(strings).getBuffer()+unitIndex, length);
}
int32_t
UCharsTrieBuilder::writeValueAndFinal(int32_t i, UBool isFinal) {
if(0<=i && i<=UCharsTrie::kMaxOneUnitValue) {
return write(i|(isFinal<<15));
}
char16_t intUnits[3];
int32_t length;
if(i<0 || i>UCharsTrie::kMaxTwoUnitValue) {
intUnits[0]=(char16_t)(UCharsTrie::kThreeUnitValueLead);
intUnits[1]=(char16_t)((uint32_t)i>>16);
intUnits[2]=(char16_t)i;
length=3;
// } else if(i<=UCharsTrie::kMaxOneUnitValue) {
// intUnits[0]=(char16_t)(i);
// length=1;
} else {
intUnits[0]=(char16_t)(UCharsTrie::kMinTwoUnitValueLead+(i>>16));
intUnits[1]=(char16_t)i;
length=2;
}
intUnits[0]=(char16_t)(intUnits[0]|(isFinal<<15));
return write(intUnits, length);
}
int32_t
UCharsTrieBuilder::writeValueAndType(UBool hasValue, int32_t value, int32_t node) {
if(!hasValue) {
return write(node);
}
char16_t intUnits[3];
int32_t length;
if(value<0 || value>UCharsTrie::kMaxTwoUnitNodeValue) {
intUnits[0]=(char16_t)(UCharsTrie::kThreeUnitNodeValueLead);
intUnits[1]=(char16_t)((uint32_t)value>>16);
intUnits[2]=(char16_t)value;
length=3;
} else if(value<=UCharsTrie::kMaxOneUnitNodeValue) {
intUnits[0]=(char16_t)((value+1)<<6);
length=1;
} else {
intUnits[0]=(char16_t)(UCharsTrie::kMinTwoUnitNodeValueLead+((value>>10)&0x7fc0));
intUnits[1]=(char16_t)value;
length=2;
}
intUnits[0]|=(char16_t)node;
return write(intUnits, length);
}
int32_t
UCharsTrieBuilder::writeDeltaTo(int32_t jumpTarget) {
int32_t i=ucharsLength-jumpTarget;
U_ASSERT(i>=0);
if(i<=UCharsTrie::kMaxOneUnitDelta) {
return write(i);
}
char16_t intUnits[3];
int32_t length;
if(i<=UCharsTrie::kMaxTwoUnitDelta) {
intUnits[0]=(char16_t)(UCharsTrie::kMinTwoUnitDeltaLead+(i>>16));
length=1;
} else {
intUnits[0]=(char16_t)(UCharsTrie::kThreeUnitDeltaLead);
intUnits[1]=(char16_t)(i>>16);
length=2;
}
intUnits[length++]=(char16_t)i;
return write(intUnits, length);
}
U_NAMESPACE_END
|