2 *******************************************************************************
3 * Copyright (C) 2010-2012, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *******************************************************************************
6 * file name: bytestriebuilder.cpp
8 * tab size: 8 (not used)
11 * created on: 2010sep25
12 * created by: Markus W. Scherer
15 #include "unicode/utypes.h"
16 #include "unicode/bytestrie.h"
17 #include "unicode/bytestriebuilder.h"
18 #include "unicode/stringpiece.h"
29 * Note: This builder implementation stores (bytes, value) pairs with full copies
30 * of the byte sequences, until the BytesTrie is built.
31 * It might(!) take less memory if we collected the data in a temporary, dynamic trie.
34 class BytesTrieElement
: public UMemory
{
36 // Use compiler's default constructor, initializes nothing.
38 void setTo(const StringPiece
&s
, int32_t val
, CharString
&strings
, UErrorCode
&errorCode
);
40 StringPiece
getString(const CharString
&strings
) const {
41 int32_t offset
=stringOffset
;
44 length
=(uint8_t)strings
[offset
++];
47 length
=((int32_t)(uint8_t)strings
[offset
]<<8)|(uint8_t)strings
[offset
+1];
50 return StringPiece(strings
.data()+offset
, length
);
52 int32_t getStringLength(const CharString
&strings
) const {
53 int32_t offset
=stringOffset
;
55 return (uint8_t)strings
[offset
];
58 return ((int32_t)(uint8_t)strings
[offset
]<<8)|(uint8_t)strings
[offset
+1];
62 char charAt(int32_t index
, const CharString
&strings
) const { return data(strings
)[index
]; }
64 int32_t getValue() const { return value
; }
66 int32_t compareStringTo(const BytesTrieElement
&o
, const CharString
&strings
) const;
69 const char *data(const CharString
&strings
) const {
70 int32_t offset
=stringOffset
;
76 return strings
.data()+offset
;
79 // If the stringOffset is non-negative, then the first strings byte contains
81 // If the stringOffset is negative, then the first two strings bytes contain
82 // the string length (big-endian), and the offset needs to be bit-inverted.
83 // (Compared with a stringLength field here, this saves 3 bytes per string for most strings.)
89 BytesTrieElement::setTo(const StringPiece
&s
, int32_t val
,
90 CharString
&strings
, UErrorCode
&errorCode
) {
91 if(U_FAILURE(errorCode
)) {
94 int32_t length
=s
.length();
96 // Too long: We store the length in 1 or 2 bytes.
97 errorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
100 int32_t offset
=strings
.length();
103 strings
.append((char)(length
>>8), errorCode
);
105 strings
.append((char)length
, errorCode
);
108 strings
.append(s
, errorCode
);
112 BytesTrieElement::compareStringTo(const BytesTrieElement
&other
, const CharString
&strings
) const {
113 // TODO: add StringPiece::compare(), see ticket #8187
114 StringPiece thisString
=getString(strings
);
115 StringPiece otherString
=other
.getString(strings
);
116 int32_t lengthDiff
=thisString
.length()-otherString
.length();
117 int32_t commonLength
;
119 commonLength
=thisString
.length();
121 commonLength
=otherString
.length();
123 int32_t diff
=uprv_memcmp(thisString
.data(), otherString
.data(), commonLength
);
124 return diff
!=0 ? diff
: lengthDiff
;
127 BytesTrieBuilder::BytesTrieBuilder(UErrorCode
&errorCode
)
128 : strings(NULL
), elements(NULL
), elementsCapacity(0), elementsLength(0),
129 bytes(NULL
), bytesCapacity(0), bytesLength(0) {
130 if(U_FAILURE(errorCode
)) {
133 strings
=new CharString();
135 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
139 BytesTrieBuilder::~BytesTrieBuilder() {
146 BytesTrieBuilder::add(const StringPiece
&s
, int32_t value
, UErrorCode
&errorCode
) {
147 if(U_FAILURE(errorCode
)) {
151 // Cannot add elements after building.
152 errorCode
=U_NO_WRITE_PERMISSION
;
155 if(elementsLength
==elementsCapacity
) {
157 if(elementsCapacity
==0) {
160 newCapacity
=4*elementsCapacity
;
162 BytesTrieElement
*newElements
=new BytesTrieElement
[newCapacity
];
163 if(newElements
==NULL
) {
164 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
165 return *this; // error instead of dereferencing null
167 if(elementsLength
>0) {
168 uprv_memcpy(newElements
, elements
, elementsLength
*sizeof(BytesTrieElement
));
171 elements
=newElements
;
172 elementsCapacity
=newCapacity
;
174 elements
[elementsLength
++].setTo(s
, value
, *strings
, errorCode
);
180 static int32_t U_CALLCONV
181 compareElementStrings(const void *context
, const void *left
, const void *right
) {
182 const CharString
*strings
=static_cast<const CharString
*>(context
);
183 const BytesTrieElement
*leftElement
=static_cast<const BytesTrieElement
*>(left
);
184 const BytesTrieElement
*rightElement
=static_cast<const BytesTrieElement
*>(right
);
185 return leftElement
->compareStringTo(*rightElement
, *strings
);
191 BytesTrieBuilder::build(UStringTrieBuildOption buildOption
, UErrorCode
&errorCode
) {
192 buildBytes(buildOption
, errorCode
);
193 BytesTrie
*newTrie
=NULL
;
194 if(U_SUCCESS(errorCode
)) {
195 newTrie
=new BytesTrie(bytes
, bytes
+(bytesCapacity
-bytesLength
));
197 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
199 bytes
=NULL
; // The new trie now owns the array.
207 BytesTrieBuilder::buildStringPiece(UStringTrieBuildOption buildOption
, UErrorCode
&errorCode
) {
208 buildBytes(buildOption
, errorCode
);
210 if(U_SUCCESS(errorCode
)) {
211 result
.set(bytes
+(bytesCapacity
-bytesLength
), bytesLength
);
217 BytesTrieBuilder::buildBytes(UStringTrieBuildOption buildOption
, UErrorCode
&errorCode
) {
218 if(U_FAILURE(errorCode
)) {
221 if(bytes
!=NULL
&& bytesLength
>0) {
226 if(elementsLength
==0) {
227 errorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
230 uprv_sortArray(elements
, elementsLength
, (int32_t)sizeof(BytesTrieElement
),
231 compareElementStrings
, strings
,
232 FALSE
, // need not be a stable sort
234 if(U_FAILURE(errorCode
)) {
237 // Duplicate strings are not allowed.
238 StringPiece prev
=elements
[0].getString(*strings
);
239 for(int32_t i
=1; i
<elementsLength
; ++i
) {
240 StringPiece current
=elements
[i
].getString(*strings
);
242 errorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
248 // Create and byte-serialize the trie for the elements.
250 int32_t capacity
=strings
->length();
254 if(bytesCapacity
<capacity
) {
256 bytes
=static_cast<char *>(uprv_malloc(capacity
));
258 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
262 bytesCapacity
=capacity
;
264 StringTrieBuilder::build(buildOption
, elementsLength
, errorCode
);
266 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
271 BytesTrieBuilder::clear() {
279 BytesTrieBuilder::getElementStringLength(int32_t i
) const {
280 return elements
[i
].getStringLength(*strings
);
284 BytesTrieBuilder::getElementUnit(int32_t i
, int32_t byteIndex
) const {
285 return (uint8_t)elements
[i
].charAt(byteIndex
, *strings
);
289 BytesTrieBuilder::getElementValue(int32_t i
) const {
290 return elements
[i
].getValue();
294 BytesTrieBuilder::getLimitOfLinearMatch(int32_t first
, int32_t last
, int32_t byteIndex
) const {
295 const BytesTrieElement
&firstElement
=elements
[first
];
296 const BytesTrieElement
&lastElement
=elements
[last
];
297 int32_t minStringLength
=firstElement
.getStringLength(*strings
);
298 while(++byteIndex
<minStringLength
&&
299 firstElement
.charAt(byteIndex
, *strings
)==
300 lastElement
.charAt(byteIndex
, *strings
)) {}
305 BytesTrieBuilder::countElementUnits(int32_t start
, int32_t limit
, int32_t byteIndex
) const {
306 int32_t length
=0; // Number of different bytes at byteIndex.
309 char byte
=elements
[i
++].charAt(byteIndex
, *strings
);
310 while(i
<limit
&& byte
==elements
[i
].charAt(byteIndex
, *strings
)) {
319 BytesTrieBuilder::skipElementsBySomeUnits(int32_t i
, int32_t byteIndex
, int32_t count
) const {
321 char byte
=elements
[i
++].charAt(byteIndex
, *strings
);
322 while(byte
==elements
[i
].charAt(byteIndex
, *strings
)) {
330 BytesTrieBuilder::indexOfElementWithNextUnit(int32_t i
, int32_t byteIndex
, UChar byte
) const {
332 while(b
==elements
[i
].charAt(byteIndex
, *strings
)) {
338 BytesTrieBuilder::BTLinearMatchNode::BTLinearMatchNode(const char *bytes
, int32_t len
, Node
*nextNode
)
339 : LinearMatchNode(len
, nextNode
), s(bytes
) {
340 hash
=hash
*37+ustr_hashCharsN(bytes
, len
);
344 BytesTrieBuilder::BTLinearMatchNode::operator==(const Node
&other
) const {
348 if(!LinearMatchNode::operator==(other
)) {
351 const BTLinearMatchNode
&o
=(const BTLinearMatchNode
&)other
;
352 return 0==uprv_memcmp(s
, o
.s
, length
);
356 BytesTrieBuilder::BTLinearMatchNode::write(StringTrieBuilder
&builder
) {
357 BytesTrieBuilder
&b
=(BytesTrieBuilder
&)builder
;
358 next
->write(builder
);
360 offset
=b
.write(b
.getMinLinearMatch()+length
-1);
363 StringTrieBuilder::Node
*
364 BytesTrieBuilder::createLinearMatchNode(int32_t i
, int32_t byteIndex
, int32_t length
,
365 Node
*nextNode
) const {
366 return new BTLinearMatchNode(
367 elements
[i
].getString(*strings
).data()+byteIndex
,
373 BytesTrieBuilder::ensureCapacity(int32_t length
) {
375 return FALSE
; // previous memory allocation had failed
377 if(length
>bytesCapacity
) {
378 int32_t newCapacity
=bytesCapacity
;
381 } while(newCapacity
<=length
);
382 char *newBytes
=static_cast<char *>(uprv_malloc(newCapacity
));
384 // unable to allocate memory
390 uprv_memcpy(newBytes
+(newCapacity
-bytesLength
),
391 bytes
+(bytesCapacity
-bytesLength
), bytesLength
);
394 bytesCapacity
=newCapacity
;
400 BytesTrieBuilder::write(int32_t byte
) {
401 int32_t newLength
=bytesLength
+1;
402 if(ensureCapacity(newLength
)) {
403 bytesLength
=newLength
;
404 bytes
[bytesCapacity
-bytesLength
]=(char)byte
;
410 BytesTrieBuilder::write(const char *b
, int32_t length
) {
411 int32_t newLength
=bytesLength
+length
;
412 if(ensureCapacity(newLength
)) {
413 bytesLength
=newLength
;
414 uprv_memcpy(bytes
+(bytesCapacity
-bytesLength
), b
, length
);
420 BytesTrieBuilder::writeElementUnits(int32_t i
, int32_t byteIndex
, int32_t length
) {
421 return write(elements
[i
].getString(*strings
).data()+byteIndex
, length
);
425 BytesTrieBuilder::writeValueAndFinal(int32_t i
, UBool isFinal
) {
426 if(0<=i
&& i
<=BytesTrie::kMaxOneByteValue
) {
427 return write(((BytesTrie::kMinOneByteValueLead
+i
)<<1)|isFinal
);
431 if(i
<0 || i
>0xffffff) {
432 intBytes
[0]=(char)BytesTrie::kFiveByteValueLead
;
433 intBytes
[1]=(char)((uint32_t)i
>>24);
434 intBytes
[2]=(char)((uint32_t)i
>>16);
435 intBytes
[3]=(char)((uint32_t)i
>>8);
438 // } else if(i<=BytesTrie::kMaxOneByteValue) {
439 // intBytes[0]=(char)(BytesTrie::kMinOneByteValueLead+i);
441 if(i
<=BytesTrie::kMaxTwoByteValue
) {
442 intBytes
[0]=(char)(BytesTrie::kMinTwoByteValueLead
+(i
>>8));
444 if(i
<=BytesTrie::kMaxThreeByteValue
) {
445 intBytes
[0]=(char)(BytesTrie::kMinThreeByteValueLead
+(i
>>16));
447 intBytes
[0]=(char)BytesTrie::kFourByteValueLead
;
448 intBytes
[1]=(char)(i
>>16);
451 intBytes
[length
++]=(char)(i
>>8);
453 intBytes
[length
++]=(char)i
;
455 intBytes
[0]=(char)((intBytes
[0]<<1)|isFinal
);
456 return write(intBytes
, length
);
460 BytesTrieBuilder::writeValueAndType(UBool hasValue
, int32_t value
, int32_t node
) {
461 int32_t offset
=write(node
);
463 offset
=writeValueAndFinal(value
, FALSE
);
469 BytesTrieBuilder::writeDeltaTo(int32_t jumpTarget
) {
470 int32_t i
=bytesLength
-jumpTarget
;
472 if(i
<=BytesTrie::kMaxOneByteDelta
) {
477 if(i
<=BytesTrie::kMaxTwoByteDelta
) {
478 intBytes
[0]=(char)(BytesTrie::kMinTwoByteDeltaLead
+(i
>>8));
481 if(i
<=BytesTrie::kMaxThreeByteDelta
) {
482 intBytes
[0]=(char)(BytesTrie::kMinThreeByteDeltaLead
+(i
>>16));
486 intBytes
[0]=(char)BytesTrie::kFourByteDeltaLead
;
489 intBytes
[0]=(char)BytesTrie::kFiveByteDeltaLead
;
490 intBytes
[1]=(char)(i
>>24);
493 intBytes
[1]=(char)(i
>>16);
495 intBytes
[1]=(char)(i
>>8);
497 intBytes
[length
++]=(char)i
;
498 return write(intBytes
, length
);