|
24 | 24 | A BidiTrieContainer is mostly a large buffer in which distinct but related |
25 | 25 | tries are stored. The memory layout of the buffer is as follow: |
26 | 26 |
|
27 | | - 0-2047: haystack section |
28 | | - 2048-2051: number of significant characters in the haystack |
29 | | - 2052-2055: offset to start of trie data section (=> trie0) |
30 | | - 2056-2059: offset to end of trie data section (=> trie1) |
31 | | - 2060-2063: offset to start of character data section (=> char0) |
32 | | - 2064-2067: offset to end of character data section (=> char1) |
33 | | - 2068: start of trie data section |
| 27 | + 0-8192: haystack section |
| 28 | + 8192-8195: number of significant characters in the haystack |
| 29 | + 8196-8199: offset to start of trie data section (=> trie0) |
| 30 | + 8200-8203: offset to end of trie data section (=> trie1) |
| 31 | + 8204-8207: offset to start of character data section (=> char0) |
| 32 | + 8208-8211: offset to end of character data section (=> char1) |
| 33 | + 8212-8215: offset to left index result (=> result_l) |
| 34 | + 8216-8219: offset to right index result (=> result_r) |
| 35 | + 8220-8223: offset to extra unit result (=> result_iu) |
| 36 | + 8224: start of trie data section |
34 | 37 |
|
35 | 38 | +--------------+ |
36 | 39 | Normal cell: | And | If "Segment info" matches: |
|
93 | 96 |
|
94 | 97 | */ |
95 | 98 |
|
| 99 | +const VERSION = 2; |
96 | 100 | const PAGE_SIZE = 65536*2; |
97 | 101 | const HAYSTACK_START = 0; |
98 | | -const HAYSTACK_SIZE = 2048; // i32 / i8 |
99 | | -const HAYSTACK_SIZE_SLOT = HAYSTACK_SIZE >>> 2; // 512 / 2048 |
100 | | -const TRIE0_SLOT = HAYSTACK_SIZE_SLOT + 1; // 513 / 2052 |
101 | | -const TRIE1_SLOT = HAYSTACK_SIZE_SLOT + 2; // 514 / 2056 |
102 | | -const CHAR0_SLOT = HAYSTACK_SIZE_SLOT + 3; // 515 / 2060 |
103 | | -const CHAR1_SLOT = HAYSTACK_SIZE_SLOT + 4; // 516 / 2064 |
104 | | -const RESULT_L_SLOT = HAYSTACK_SIZE_SLOT + 5; // 517 / 2068 |
105 | | -const RESULT_R_SLOT = HAYSTACK_SIZE_SLOT + 6; // 518 / 2072 |
106 | | -const RESULT_IU_SLOT = HAYSTACK_SIZE_SLOT + 7; // 519 / 2076 |
107 | | -const TRIE0_START = HAYSTACK_SIZE_SLOT + 8 << 2; // 2080 |
| 102 | +const HAYSTACK_SIZE = 8192; // i32 / i8 |
| 103 | +const HAYSTACK_SIZE_SLOT = HAYSTACK_SIZE >>> 2; // 2048 / 8192 |
| 104 | +const TRIE0_SLOT = HAYSTACK_SIZE_SLOT + 1; // 2049 / 8196 |
| 105 | +const TRIE1_SLOT = HAYSTACK_SIZE_SLOT + 2; // 2050 / 8200 |
| 106 | +const CHAR0_SLOT = HAYSTACK_SIZE_SLOT + 3; // 2051 / 8204 |
| 107 | +const CHAR1_SLOT = HAYSTACK_SIZE_SLOT + 4; // 2052 / 8208 |
| 108 | +const RESULT_L_SLOT = HAYSTACK_SIZE_SLOT + 5; // 2053 / 8212 |
| 109 | +const RESULT_R_SLOT = HAYSTACK_SIZE_SLOT + 6; // 2054 / 8216 |
| 110 | +const RESULT_IU_SLOT = HAYSTACK_SIZE_SLOT + 7; // 2055 / 8220 |
| 111 | +const TRIE0_START = HAYSTACK_SIZE_SLOT + 8 << 2; // 8224 |
108 | 112 |
|
109 | 113 | const CELL_BYTE_LENGTH = 12; |
110 | 114 | const MIN_FREE_CELL_BYTE_LENGTH = CELL_BYTE_LENGTH * 8; |
@@ -156,28 +160,21 @@ class BidiTrieContainer { |
156 | 160 | // Public methods |
157 | 161 | //-------------------------------------------------------------------------- |
158 | 162 |
|
159 | | - get haystackLen() { |
| 163 | + getHaystackLen() { |
160 | 164 | return this.buf32[HAYSTACK_SIZE_SLOT]; |
161 | 165 | } |
162 | 166 |
|
163 | | - set haystackLen(v) { |
| 167 | + setHaystackLen(v) { |
| 168 | + if ( v > HAYSTACK_SIZE ) { |
| 169 | + v = HAYSTACK_SIZE; |
| 170 | + } |
164 | 171 | this.buf32[HAYSTACK_SIZE_SLOT] = v; |
| 172 | + return v; |
165 | 173 | } |
166 | 174 |
|
167 | | - reset(details) { |
168 | | - if ( |
169 | | - details instanceof Object && |
170 | | - typeof details.byteLength === 'number' && |
171 | | - typeof details.char0 === 'number' |
172 | | - ) { |
173 | | - if ( details.byteLength > this.buf8.byteLength ) { |
174 | | - this.reallocateBuf(details.byteLength); |
175 | | - } |
176 | | - this.buf32[CHAR0_SLOT] = details.char0; |
177 | | - } |
| 175 | + reset() { |
178 | 176 | this.buf32[TRIE1_SLOT] = this.buf32[TRIE0_SLOT]; |
179 | 177 | this.buf32[CHAR1_SLOT] = this.buf32[CHAR0_SLOT]; |
180 | | - |
181 | 178 | this.lastStored = ''; |
182 | 179 | this.lastStoredLen = this.lastStoredIndex = 0; |
183 | 180 | } |
@@ -571,23 +568,22 @@ class BidiTrieContainer { |
571 | 568 | this.buf32[iboundary+BCELL_EXTRA] = v; |
572 | 569 | } |
573 | 570 |
|
574 | | - optimize(shrink = false) { |
575 | | - if ( shrink ) { |
576 | | - this.shrinkBuf(); |
577 | | - } |
578 | | - return { |
579 | | - byteLength: this.buf8.byteLength, |
580 | | - char0: this.buf32[CHAR0_SLOT], |
581 | | - }; |
| 571 | + optimize() { |
| 572 | + this.shrinkBuf(); |
582 | 573 | } |
583 | 574 |
|
584 | 575 | toSelfie() { |
585 | 576 | const buf32 = this.buf32.subarray(0, this.buf32[CHAR1_SLOT] + 3 >>> 2); |
586 | | - return { buf32, checksum: i32Checksum(buf32) }; |
| 577 | + return { |
| 578 | + version: VERSION, |
| 579 | + buf32, |
| 580 | + checksum: i32Checksum(buf32), |
| 581 | + }; |
587 | 582 | } |
588 | 583 |
|
589 | 584 | fromSelfie(selfie) { |
590 | 585 | if ( typeof selfie !== 'object' || selfie === null ) { return false; } |
| 586 | + if ( selfie.version !== VERSION ) { return false; } |
591 | 587 | if ( selfie.buf32 instanceof Uint32Array === false ) { return false; } |
592 | 588 | if ( selfie.checksum !== i32Checksum(selfie.buf32) ) { return false; } |
593 | 589 | const byteLength = selfie.buf32.length << 2; |
|
0 commit comments