Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | ******************************************************************************* |
michael@0 | 3 | * Copyright (C) 2010-2012, International Business Machines |
michael@0 | 4 | * Corporation and others. All Rights Reserved. |
michael@0 | 5 | ******************************************************************************* |
michael@0 | 6 | * file name: bytestrie.h |
michael@0 | 7 | * encoding: US-ASCII |
michael@0 | 8 | * tab size: 8 (not used) |
michael@0 | 9 | * indentation:4 |
michael@0 | 10 | * |
michael@0 | 11 | * created on: 2010sep25 |
michael@0 | 12 | * created by: Markus W. Scherer |
michael@0 | 13 | */ |
michael@0 | 14 | |
michael@0 | 15 | #ifndef __BYTESTRIE_H__ |
michael@0 | 16 | #define __BYTESTRIE_H__ |
michael@0 | 17 | |
michael@0 | 18 | /** |
michael@0 | 19 | * \file |
michael@0 | 20 | * \brief C++ API: Trie for mapping byte sequences to integer values. |
michael@0 | 21 | */ |
michael@0 | 22 | |
michael@0 | 23 | #include "unicode/utypes.h" |
michael@0 | 24 | #include "unicode/stringpiece.h" |
michael@0 | 25 | #include "unicode/uobject.h" |
michael@0 | 26 | #include "unicode/ustringtrie.h" |
michael@0 | 27 | |
michael@0 | 28 | U_NAMESPACE_BEGIN |
michael@0 | 29 | |
michael@0 | 30 | class ByteSink; |
michael@0 | 31 | class BytesTrieBuilder; |
michael@0 | 32 | class CharString; |
michael@0 | 33 | class UVector32; |
michael@0 | 34 | |
michael@0 | 35 | /** |
michael@0 | 36 | * Light-weight, non-const reader class for a BytesTrie. |
michael@0 | 37 | * Traverses a byte-serialized data structure with minimal state, |
michael@0 | 38 | * for mapping byte sequences to non-negative integer values. |
michael@0 | 39 | * |
michael@0 | 40 | * This class owns the serialized trie data only if it was constructed by |
michael@0 | 41 | * the builder's build() method. |
michael@0 | 42 | * The public constructor and the copy constructor only alias the data (only copy the pointer). |
michael@0 | 43 | * There is no assignment operator. |
michael@0 | 44 | * |
michael@0 | 45 | * This class is not intended for public subclassing. |
michael@0 | 46 | * @stable ICU 4.8 |
michael@0 | 47 | */ |
michael@0 | 48 | class U_COMMON_API BytesTrie : public UMemory { |
michael@0 | 49 | public: |
michael@0 | 50 | /** |
michael@0 | 51 | * Constructs a BytesTrie reader instance. |
michael@0 | 52 | * |
michael@0 | 53 | * The trieBytes must contain a copy of a byte sequence from the BytesTrieBuilder, |
michael@0 | 54 | * starting with the first byte of that sequence. |
michael@0 | 55 | * The BytesTrie object will not read more bytes than |
michael@0 | 56 | * the BytesTrieBuilder generated in the corresponding build() call. |
michael@0 | 57 | * |
michael@0 | 58 | * The array is not copied/cloned and must not be modified while |
michael@0 | 59 | * the BytesTrie object is in use. |
michael@0 | 60 | * |
michael@0 | 61 | * @param trieBytes The byte array that contains the serialized trie. |
michael@0 | 62 | * @stable ICU 4.8 |
michael@0 | 63 | */ |
michael@0 | 64 | BytesTrie(const void *trieBytes) |
michael@0 | 65 | : ownedArray_(NULL), bytes_(static_cast<const uint8_t *>(trieBytes)), |
michael@0 | 66 | pos_(bytes_), remainingMatchLength_(-1) {} |
michael@0 | 67 | |
michael@0 | 68 | /** |
michael@0 | 69 | * Destructor. |
michael@0 | 70 | * @stable ICU 4.8 |
michael@0 | 71 | */ |
michael@0 | 72 | ~BytesTrie(); |
michael@0 | 73 | |
michael@0 | 74 | /** |
michael@0 | 75 | * Copy constructor, copies the other trie reader object and its state, |
michael@0 | 76 | * but not the byte array which will be shared. (Shallow copy.) |
michael@0 | 77 | * @param other Another BytesTrie object. |
michael@0 | 78 | * @stable ICU 4.8 |
michael@0 | 79 | */ |
michael@0 | 80 | BytesTrie(const BytesTrie &other) |
michael@0 | 81 | : ownedArray_(NULL), bytes_(other.bytes_), |
michael@0 | 82 | pos_(other.pos_), remainingMatchLength_(other.remainingMatchLength_) {} |
michael@0 | 83 | |
michael@0 | 84 | /** |
michael@0 | 85 | * Resets this trie to its initial state. |
michael@0 | 86 | * @return *this |
michael@0 | 87 | * @stable ICU 4.8 |
michael@0 | 88 | */ |
michael@0 | 89 | BytesTrie &reset() { |
michael@0 | 90 | pos_=bytes_; |
michael@0 | 91 | remainingMatchLength_=-1; |
michael@0 | 92 | return *this; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | /** |
michael@0 | 96 | * BytesTrie state object, for saving a trie's current state |
michael@0 | 97 | * and resetting the trie back to this state later. |
michael@0 | 98 | * @stable ICU 4.8 |
michael@0 | 99 | */ |
michael@0 | 100 | class State : public UMemory { |
michael@0 | 101 | public: |
michael@0 | 102 | /** |
michael@0 | 103 | * Constructs an empty State. |
michael@0 | 104 | * @stable ICU 4.8 |
michael@0 | 105 | */ |
michael@0 | 106 | State() { bytes=NULL; } |
michael@0 | 107 | private: |
michael@0 | 108 | friend class BytesTrie; |
michael@0 | 109 | |
michael@0 | 110 | const uint8_t *bytes; |
michael@0 | 111 | const uint8_t *pos; |
michael@0 | 112 | int32_t remainingMatchLength; |
michael@0 | 113 | }; |
michael@0 | 114 | |
michael@0 | 115 | /** |
michael@0 | 116 | * Saves the state of this trie. |
michael@0 | 117 | * @param state The State object to hold the trie's state. |
michael@0 | 118 | * @return *this |
michael@0 | 119 | * @see resetToState |
michael@0 | 120 | * @stable ICU 4.8 |
michael@0 | 121 | */ |
michael@0 | 122 | const BytesTrie &saveState(State &state) const { |
michael@0 | 123 | state.bytes=bytes_; |
michael@0 | 124 | state.pos=pos_; |
michael@0 | 125 | state.remainingMatchLength=remainingMatchLength_; |
michael@0 | 126 | return *this; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | /** |
michael@0 | 130 | * Resets this trie to the saved state. |
michael@0 | 131 | * If the state object contains no state, or the state of a different trie, |
michael@0 | 132 | * then this trie remains unchanged. |
michael@0 | 133 | * @param state The State object which holds a saved trie state. |
michael@0 | 134 | * @return *this |
michael@0 | 135 | * @see saveState |
michael@0 | 136 | * @see reset |
michael@0 | 137 | * @stable ICU 4.8 |
michael@0 | 138 | */ |
michael@0 | 139 | BytesTrie &resetToState(const State &state) { |
michael@0 | 140 | if(bytes_==state.bytes && bytes_!=NULL) { |
michael@0 | 141 | pos_=state.pos; |
michael@0 | 142 | remainingMatchLength_=state.remainingMatchLength; |
michael@0 | 143 | } |
michael@0 | 144 | return *this; |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | /** |
michael@0 | 148 | * Determines whether the byte sequence so far matches, whether it has a value, |
michael@0 | 149 | * and whether another input byte can continue a matching byte sequence. |
michael@0 | 150 | * @return The match/value Result. |
michael@0 | 151 | * @stable ICU 4.8 |
michael@0 | 152 | */ |
michael@0 | 153 | UStringTrieResult current() const; |
michael@0 | 154 | |
michael@0 | 155 | /** |
michael@0 | 156 | * Traverses the trie from the initial state for this input byte. |
michael@0 | 157 | * Equivalent to reset().next(inByte). |
michael@0 | 158 | * @param inByte Input byte value. Values -0x100..-1 are treated like 0..0xff. |
michael@0 | 159 | * Values below -0x100 and above 0xff will never match. |
michael@0 | 160 | * @return The match/value Result. |
michael@0 | 161 | * @stable ICU 4.8 |
michael@0 | 162 | */ |
michael@0 | 163 | inline UStringTrieResult first(int32_t inByte) { |
michael@0 | 164 | remainingMatchLength_=-1; |
michael@0 | 165 | if(inByte<0) { |
michael@0 | 166 | inByte+=0x100; |
michael@0 | 167 | } |
michael@0 | 168 | return nextImpl(bytes_, inByte); |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | /** |
michael@0 | 172 | * Traverses the trie from the current state for this input byte. |
michael@0 | 173 | * @param inByte Input byte value. Values -0x100..-1 are treated like 0..0xff. |
michael@0 | 174 | * Values below -0x100 and above 0xff will never match. |
michael@0 | 175 | * @return The match/value Result. |
michael@0 | 176 | * @stable ICU 4.8 |
michael@0 | 177 | */ |
michael@0 | 178 | UStringTrieResult next(int32_t inByte); |
michael@0 | 179 | |
michael@0 | 180 | /** |
michael@0 | 181 | * Traverses the trie from the current state for this byte sequence. |
michael@0 | 182 | * Equivalent to |
michael@0 | 183 | * \code |
michael@0 | 184 | * Result result=current(); |
michael@0 | 185 | * for(each c in s) |
michael@0 | 186 | * if(!USTRINGTRIE_HAS_NEXT(result)) return USTRINGTRIE_NO_MATCH; |
michael@0 | 187 | * result=next(c); |
michael@0 | 188 | * return result; |
michael@0 | 189 | * \endcode |
michael@0 | 190 | * @param s A string or byte sequence. Can be NULL if length is 0. |
michael@0 | 191 | * @param length The length of the byte sequence. Can be -1 if NUL-terminated. |
michael@0 | 192 | * @return The match/value Result. |
michael@0 | 193 | * @stable ICU 4.8 |
michael@0 | 194 | */ |
michael@0 | 195 | UStringTrieResult next(const char *s, int32_t length); |
michael@0 | 196 | |
michael@0 | 197 | /** |
michael@0 | 198 | * Returns a matching byte sequence's value if called immediately after |
michael@0 | 199 | * current()/first()/next() returned USTRINGTRIE_INTERMEDIATE_VALUE or USTRINGTRIE_FINAL_VALUE. |
michael@0 | 200 | * getValue() can be called multiple times. |
michael@0 | 201 | * |
michael@0 | 202 | * Do not call getValue() after USTRINGTRIE_NO_MATCH or USTRINGTRIE_NO_VALUE! |
michael@0 | 203 | * @return The value for the byte sequence so far. |
michael@0 | 204 | * @stable ICU 4.8 |
michael@0 | 205 | */ |
michael@0 | 206 | inline int32_t getValue() const { |
michael@0 | 207 | const uint8_t *pos=pos_; |
michael@0 | 208 | int32_t leadByte=*pos++; |
michael@0 | 209 | // U_ASSERT(leadByte>=kMinValueLead); |
michael@0 | 210 | return readValue(pos, leadByte>>1); |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | /** |
michael@0 | 214 | * Determines whether all byte sequences reachable from the current state |
michael@0 | 215 | * map to the same value. |
michael@0 | 216 | * @param uniqueValue Receives the unique value, if this function returns TRUE. |
michael@0 | 217 | * (output-only) |
michael@0 | 218 | * @return TRUE if all byte sequences reachable from the current state |
michael@0 | 219 | * map to the same value. |
michael@0 | 220 | * @stable ICU 4.8 |
michael@0 | 221 | */ |
michael@0 | 222 | inline UBool hasUniqueValue(int32_t &uniqueValue) const { |
michael@0 | 223 | const uint8_t *pos=pos_; |
michael@0 | 224 | // Skip the rest of a pending linear-match node. |
michael@0 | 225 | return pos!=NULL && findUniqueValue(pos+remainingMatchLength_+1, FALSE, uniqueValue); |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | /** |
michael@0 | 229 | * Finds each byte which continues the byte sequence from the current state. |
michael@0 | 230 | * That is, each byte b for which it would be next(b)!=USTRINGTRIE_NO_MATCH now. |
michael@0 | 231 | * @param out Each next byte is appended to this object. |
michael@0 | 232 | * (Only uses the out.Append(s, length) method.) |
michael@0 | 233 | * @return the number of bytes which continue the byte sequence from here |
michael@0 | 234 | * @stable ICU 4.8 |
michael@0 | 235 | */ |
michael@0 | 236 | int32_t getNextBytes(ByteSink &out) const; |
michael@0 | 237 | |
michael@0 | 238 | /** |
michael@0 | 239 | * Iterator for all of the (byte sequence, value) pairs in a BytesTrie. |
michael@0 | 240 | * @stable ICU 4.8 |
michael@0 | 241 | */ |
michael@0 | 242 | class U_COMMON_API Iterator : public UMemory { |
michael@0 | 243 | public: |
michael@0 | 244 | /** |
michael@0 | 245 | * Iterates from the root of a byte-serialized BytesTrie. |
michael@0 | 246 | * @param trieBytes The trie bytes. |
michael@0 | 247 | * @param maxStringLength If 0, the iterator returns full strings/byte sequences. |
michael@0 | 248 | * Otherwise, the iterator returns strings with this maximum length. |
michael@0 | 249 | * @param errorCode Standard ICU error code. Its input value must |
michael@0 | 250 | * pass the U_SUCCESS() test, or else the function returns |
michael@0 | 251 | * immediately. Check for U_FAILURE() on output or use with |
michael@0 | 252 | * function chaining. (See User Guide for details.) |
michael@0 | 253 | * @stable ICU 4.8 |
michael@0 | 254 | */ |
michael@0 | 255 | Iterator(const void *trieBytes, int32_t maxStringLength, UErrorCode &errorCode); |
michael@0 | 256 | |
michael@0 | 257 | /** |
michael@0 | 258 | * Iterates from the current state of the specified BytesTrie. |
michael@0 | 259 | * @param trie The trie whose state will be copied for iteration. |
michael@0 | 260 | * @param maxStringLength If 0, the iterator returns full strings/byte sequences. |
michael@0 | 261 | * Otherwise, the iterator returns strings with this maximum length. |
michael@0 | 262 | * @param errorCode Standard ICU error code. Its input value must |
michael@0 | 263 | * pass the U_SUCCESS() test, or else the function returns |
michael@0 | 264 | * immediately. Check for U_FAILURE() on output or use with |
michael@0 | 265 | * function chaining. (See User Guide for details.) |
michael@0 | 266 | * @stable ICU 4.8 |
michael@0 | 267 | */ |
michael@0 | 268 | Iterator(const BytesTrie &trie, int32_t maxStringLength, UErrorCode &errorCode); |
michael@0 | 269 | |
michael@0 | 270 | /** |
michael@0 | 271 | * Destructor. |
michael@0 | 272 | * @stable ICU 4.8 |
michael@0 | 273 | */ |
michael@0 | 274 | ~Iterator(); |
michael@0 | 275 | |
michael@0 | 276 | /** |
michael@0 | 277 | * Resets this iterator to its initial state. |
michael@0 | 278 | * @return *this |
michael@0 | 279 | * @stable ICU 4.8 |
michael@0 | 280 | */ |
michael@0 | 281 | Iterator &reset(); |
michael@0 | 282 | |
michael@0 | 283 | /** |
michael@0 | 284 | * @return TRUE if there are more elements. |
michael@0 | 285 | * @stable ICU 4.8 |
michael@0 | 286 | */ |
michael@0 | 287 | UBool hasNext() const; |
michael@0 | 288 | |
michael@0 | 289 | /** |
michael@0 | 290 | * Finds the next (byte sequence, value) pair if there is one. |
michael@0 | 291 | * |
michael@0 | 292 | * If the byte sequence is truncated to the maximum length and does not |
michael@0 | 293 | * have a real value, then the value is set to -1. |
michael@0 | 294 | * In this case, this "not a real value" is indistinguishable from |
michael@0 | 295 | * a real value of -1. |
michael@0 | 296 | * @param errorCode Standard ICU error code. Its input value must |
michael@0 | 297 | * pass the U_SUCCESS() test, or else the function returns |
michael@0 | 298 | * immediately. Check for U_FAILURE() on output or use with |
michael@0 | 299 | * function chaining. (See User Guide for details.) |
michael@0 | 300 | * @return TRUE if there is another element. |
michael@0 | 301 | * @stable ICU 4.8 |
michael@0 | 302 | */ |
michael@0 | 303 | UBool next(UErrorCode &errorCode); |
michael@0 | 304 | |
michael@0 | 305 | /** |
michael@0 | 306 | * @return The NUL-terminated byte sequence for the last successful next(). |
michael@0 | 307 | * @stable ICU 4.8 |
michael@0 | 308 | */ |
michael@0 | 309 | const StringPiece &getString() const { return sp_; } |
michael@0 | 310 | /** |
michael@0 | 311 | * @return The value for the last successful next(). |
michael@0 | 312 | * @stable ICU 4.8 |
michael@0 | 313 | */ |
michael@0 | 314 | int32_t getValue() const { return value_; } |
michael@0 | 315 | |
michael@0 | 316 | private: |
michael@0 | 317 | UBool truncateAndStop(); |
michael@0 | 318 | |
michael@0 | 319 | const uint8_t *branchNext(const uint8_t *pos, int32_t length, UErrorCode &errorCode); |
michael@0 | 320 | |
michael@0 | 321 | const uint8_t *bytes_; |
michael@0 | 322 | const uint8_t *pos_; |
michael@0 | 323 | const uint8_t *initialPos_; |
michael@0 | 324 | int32_t remainingMatchLength_; |
michael@0 | 325 | int32_t initialRemainingMatchLength_; |
michael@0 | 326 | |
michael@0 | 327 | CharString *str_; |
michael@0 | 328 | StringPiece sp_; |
michael@0 | 329 | int32_t maxLength_; |
michael@0 | 330 | int32_t value_; |
michael@0 | 331 | |
michael@0 | 332 | // The stack stores pairs of integers for backtracking to another |
michael@0 | 333 | // outbound edge of a branch node. |
michael@0 | 334 | // The first integer is an offset from bytes_. |
michael@0 | 335 | // The second integer has the str_->length() from before the node in bits 15..0, |
michael@0 | 336 | // and the remaining branch length in bits 24..16. (Bits 31..25 are unused.) |
michael@0 | 337 | // (We could store the remaining branch length minus 1 in bits 23..16 and not use bits 31..24, |
michael@0 | 338 | // but the code looks more confusing that way.) |
michael@0 | 339 | UVector32 *stack_; |
michael@0 | 340 | }; |
michael@0 | 341 | |
michael@0 | 342 | private: |
michael@0 | 343 | friend class BytesTrieBuilder; |
michael@0 | 344 | |
michael@0 | 345 | /** |
michael@0 | 346 | * Constructs a BytesTrie reader instance. |
michael@0 | 347 | * Unlike the public constructor which just aliases an array, |
michael@0 | 348 | * this constructor adopts the builder's array. |
michael@0 | 349 | * This constructor is only called by the builder. |
michael@0 | 350 | */ |
michael@0 | 351 | BytesTrie(void *adoptBytes, const void *trieBytes) |
michael@0 | 352 | : ownedArray_(static_cast<uint8_t *>(adoptBytes)), |
michael@0 | 353 | bytes_(static_cast<const uint8_t *>(trieBytes)), |
michael@0 | 354 | pos_(bytes_), remainingMatchLength_(-1) {} |
michael@0 | 355 | |
michael@0 | 356 | // No assignment operator. |
michael@0 | 357 | BytesTrie &operator=(const BytesTrie &other); |
michael@0 | 358 | |
michael@0 | 359 | inline void stop() { |
michael@0 | 360 | pos_=NULL; |
michael@0 | 361 | } |
michael@0 | 362 | |
michael@0 | 363 | // Reads a compact 32-bit integer. |
michael@0 | 364 | // pos is already after the leadByte, and the lead byte is already shifted right by 1. |
michael@0 | 365 | static int32_t readValue(const uint8_t *pos, int32_t leadByte); |
michael@0 | 366 | static inline const uint8_t *skipValue(const uint8_t *pos, int32_t leadByte) { |
michael@0 | 367 | // U_ASSERT(leadByte>=kMinValueLead); |
michael@0 | 368 | if(leadByte>=(kMinTwoByteValueLead<<1)) { |
michael@0 | 369 | if(leadByte<(kMinThreeByteValueLead<<1)) { |
michael@0 | 370 | ++pos; |
michael@0 | 371 | } else if(leadByte<(kFourByteValueLead<<1)) { |
michael@0 | 372 | pos+=2; |
michael@0 | 373 | } else { |
michael@0 | 374 | pos+=3+((leadByte>>1)&1); |
michael@0 | 375 | } |
michael@0 | 376 | } |
michael@0 | 377 | return pos; |
michael@0 | 378 | } |
michael@0 | 379 | static inline const uint8_t *skipValue(const uint8_t *pos) { |
michael@0 | 380 | int32_t leadByte=*pos++; |
michael@0 | 381 | return skipValue(pos, leadByte); |
michael@0 | 382 | } |
michael@0 | 383 | |
michael@0 | 384 | // Reads a jump delta and jumps. |
michael@0 | 385 | static const uint8_t *jumpByDelta(const uint8_t *pos); |
michael@0 | 386 | |
michael@0 | 387 | static inline const uint8_t *skipDelta(const uint8_t *pos) { |
michael@0 | 388 | int32_t delta=*pos++; |
michael@0 | 389 | if(delta>=kMinTwoByteDeltaLead) { |
michael@0 | 390 | if(delta<kMinThreeByteDeltaLead) { |
michael@0 | 391 | ++pos; |
michael@0 | 392 | } else if(delta<kFourByteDeltaLead) { |
michael@0 | 393 | pos+=2; |
michael@0 | 394 | } else { |
michael@0 | 395 | pos+=3+(delta&1); |
michael@0 | 396 | } |
michael@0 | 397 | } |
michael@0 | 398 | return pos; |
michael@0 | 399 | } |
michael@0 | 400 | |
michael@0 | 401 | static inline UStringTrieResult valueResult(int32_t node) { |
michael@0 | 402 | return (UStringTrieResult)(USTRINGTRIE_INTERMEDIATE_VALUE-(node&kValueIsFinal)); |
michael@0 | 403 | } |
michael@0 | 404 | |
michael@0 | 405 | // Handles a branch node for both next(byte) and next(string). |
michael@0 | 406 | UStringTrieResult branchNext(const uint8_t *pos, int32_t length, int32_t inByte); |
michael@0 | 407 | |
michael@0 | 408 | // Requires remainingLength_<0. |
michael@0 | 409 | UStringTrieResult nextImpl(const uint8_t *pos, int32_t inByte); |
michael@0 | 410 | |
michael@0 | 411 | // Helper functions for hasUniqueValue(). |
michael@0 | 412 | // Recursively finds a unique value (or whether there is not a unique one) |
michael@0 | 413 | // from a branch. |
michael@0 | 414 | static const uint8_t *findUniqueValueFromBranch(const uint8_t *pos, int32_t length, |
michael@0 | 415 | UBool haveUniqueValue, int32_t &uniqueValue); |
michael@0 | 416 | // Recursively finds a unique value (or whether there is not a unique one) |
michael@0 | 417 | // starting from a position on a node lead byte. |
michael@0 | 418 | static UBool findUniqueValue(const uint8_t *pos, UBool haveUniqueValue, int32_t &uniqueValue); |
michael@0 | 419 | |
michael@0 | 420 | // Helper functions for getNextBytes(). |
michael@0 | 421 | // getNextBytes() when pos is on a branch node. |
michael@0 | 422 | static void getNextBranchBytes(const uint8_t *pos, int32_t length, ByteSink &out); |
michael@0 | 423 | static void append(ByteSink &out, int c); |
michael@0 | 424 | |
michael@0 | 425 | // BytesTrie data structure |
michael@0 | 426 | // |
michael@0 | 427 | // The trie consists of a series of byte-serialized nodes for incremental |
michael@0 | 428 | // string/byte sequence matching. The root node is at the beginning of the trie data. |
michael@0 | 429 | // |
michael@0 | 430 | // Types of nodes are distinguished by their node lead byte ranges. |
michael@0 | 431 | // After each node, except a final-value node, another node follows to |
michael@0 | 432 | // encode match values or continue matching further bytes. |
michael@0 | 433 | // |
michael@0 | 434 | // Node types: |
michael@0 | 435 | // - Value node: Stores a 32-bit integer in a compact, variable-length format. |
michael@0 | 436 | // The value is for the string/byte sequence so far. |
michael@0 | 437 | // One node bit indicates whether the value is final or whether |
michael@0 | 438 | // matching continues with the next node. |
michael@0 | 439 | // - Linear-match node: Matches a number of bytes. |
michael@0 | 440 | // - Branch node: Branches to other nodes according to the current input byte. |
michael@0 | 441 | // The node byte is the length of the branch (number of bytes to select from) |
michael@0 | 442 | // minus 1. It is followed by a sub-node: |
michael@0 | 443 | // - If the length is at most kMaxBranchLinearSubNodeLength, then |
michael@0 | 444 | // there are length-1 (key, value) pairs and then one more comparison byte. |
michael@0 | 445 | // If one of the key bytes matches, then the value is either a final value for |
michael@0 | 446 | // the string/byte sequence so far, or a "jump" delta to the next node. |
michael@0 | 447 | // If the last byte matches, then matching continues with the next node. |
michael@0 | 448 | // (Values have the same encoding as value nodes.) |
michael@0 | 449 | // - If the length is greater than kMaxBranchLinearSubNodeLength, then |
michael@0 | 450 | // there is one byte and one "jump" delta. |
michael@0 | 451 | // If the input byte is less than the sub-node byte, then "jump" by delta to |
michael@0 | 452 | // the next sub-node which will have a length of length/2. |
michael@0 | 453 | // (The delta has its own compact encoding.) |
michael@0 | 454 | // Otherwise, skip the "jump" delta to the next sub-node |
michael@0 | 455 | // which will have a length of length-length/2. |
michael@0 | 456 | |
michael@0 | 457 | // Node lead byte values. |
michael@0 | 458 | |
michael@0 | 459 | // 00..0f: Branch node. If node!=0 then the length is node+1, otherwise |
michael@0 | 460 | // the length is one more than the next byte. |
michael@0 | 461 | |
michael@0 | 462 | // For a branch sub-node with at most this many entries, we drop down |
michael@0 | 463 | // to a linear search. |
michael@0 | 464 | static const int32_t kMaxBranchLinearSubNodeLength=5; |
michael@0 | 465 | |
michael@0 | 466 | // 10..1f: Linear-match node, match 1..16 bytes and continue reading the next node. |
michael@0 | 467 | static const int32_t kMinLinearMatch=0x10; |
michael@0 | 468 | static const int32_t kMaxLinearMatchLength=0x10; |
michael@0 | 469 | |
michael@0 | 470 | // 20..ff: Variable-length value node. |
michael@0 | 471 | // If odd, the value is final. (Otherwise, intermediate value or jump delta.) |
michael@0 | 472 | // Then shift-right by 1 bit. |
michael@0 | 473 | // The remaining lead byte value indicates the number of following bytes (0..4) |
michael@0 | 474 | // and contains the value's top bits. |
michael@0 | 475 | static const int32_t kMinValueLead=kMinLinearMatch+kMaxLinearMatchLength; // 0x20 |
michael@0 | 476 | // It is a final value if bit 0 is set. |
michael@0 | 477 | static const int32_t kValueIsFinal=1; |
michael@0 | 478 | |
michael@0 | 479 | // Compact value: After testing bit 0, shift right by 1 and then use the following thresholds. |
michael@0 | 480 | static const int32_t kMinOneByteValueLead=kMinValueLead/2; // 0x10 |
michael@0 | 481 | static const int32_t kMaxOneByteValue=0x40; // At least 6 bits in the first byte. |
michael@0 | 482 | |
michael@0 | 483 | static const int32_t kMinTwoByteValueLead=kMinOneByteValueLead+kMaxOneByteValue+1; // 0x51 |
michael@0 | 484 | static const int32_t kMaxTwoByteValue=0x1aff; |
michael@0 | 485 | |
michael@0 | 486 | static const int32_t kMinThreeByteValueLead=kMinTwoByteValueLead+(kMaxTwoByteValue>>8)+1; // 0x6c |
michael@0 | 487 | static const int32_t kFourByteValueLead=0x7e; |
michael@0 | 488 | |
michael@0 | 489 | // A little more than Unicode code points. (0x11ffff) |
michael@0 | 490 | static const int32_t kMaxThreeByteValue=((kFourByteValueLead-kMinThreeByteValueLead)<<16)-1; |
michael@0 | 491 | |
michael@0 | 492 | static const int32_t kFiveByteValueLead=0x7f; |
michael@0 | 493 | |
michael@0 | 494 | // Compact delta integers. |
michael@0 | 495 | static const int32_t kMaxOneByteDelta=0xbf; |
michael@0 | 496 | static const int32_t kMinTwoByteDeltaLead=kMaxOneByteDelta+1; // 0xc0 |
michael@0 | 497 | static const int32_t kMinThreeByteDeltaLead=0xf0; |
michael@0 | 498 | static const int32_t kFourByteDeltaLead=0xfe; |
michael@0 | 499 | static const int32_t kFiveByteDeltaLead=0xff; |
michael@0 | 500 | |
michael@0 | 501 | static const int32_t kMaxTwoByteDelta=((kMinThreeByteDeltaLead-kMinTwoByteDeltaLead)<<8)-1; // 0x2fff |
michael@0 | 502 | static const int32_t kMaxThreeByteDelta=((kFourByteDeltaLead-kMinThreeByteDeltaLead)<<16)-1; // 0xdffff |
michael@0 | 503 | |
michael@0 | 504 | uint8_t *ownedArray_; |
michael@0 | 505 | |
michael@0 | 506 | // Fixed value referencing the BytesTrie bytes. |
michael@0 | 507 | const uint8_t *bytes_; |
michael@0 | 508 | |
michael@0 | 509 | // Iterator variables. |
michael@0 | 510 | |
michael@0 | 511 | // Pointer to next trie byte to read. NULL if no more matches. |
michael@0 | 512 | const uint8_t *pos_; |
michael@0 | 513 | // Remaining length of a linear-match node, minus 1. Negative if not in such a node. |
michael@0 | 514 | int32_t remainingMatchLength_; |
michael@0 | 515 | }; |
michael@0 | 516 | |
michael@0 | 517 | U_NAMESPACE_END |
michael@0 | 518 | |
michael@0 | 519 | #endif // __BYTESTRIE_H__ |