2 *******************************************************************************
3 * Copyright (C) 2009-2010, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 *******************************************************************************
7 package com.ibm.icu.impl;
9 import java.io.DataOutputStream;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.OutputStream;
17 * A read-only Trie2, holding 32 bit data values.
19 * A Trie2 is a highly optimized data structure for mapping from Unicode
20 * code points (values ranging from 0 to 0x10ffff) to a 16 or 32 bit value.
22 * See class Trie2 for descriptions of the API for accessing the contents of a trie.
24 * The fundamental data access methods are declared final in this class, with
25 * the intent that applications might gain a little extra performance, when compared
26 * with calling the same methods via the abstract UTrie2 base class.
29 public class Trie2_32 extends Trie2 {
32 * Internal constructor, not for general use.
39 * Create a Trie2 from its serialized form. Inverse of utrie2_serialize().
40 * The serialized format is identical between ICU4C and ICU4J, so this function
41 * will work with serialized Trie2s from either.
43 * The serialized Trie2 on the stream may be in either little or big endian byte order.
44 * This allows using serialized Tries from ICU4C without needing to consider the
45 * byte order of the system that created them.
47 * @param is an input stream to the serialized form of a UTrie2.
48 * @return An unserialized Trie_32, ready for use.
49 * @throws IllegalArgumentException if the stream does not contain a serialized Trie2.
50 * @throws IOException if a read error occurs on the InputStream.
51 * @throws ClassCastException if the stream contains a serialized Trie2_16
53 public static Trie2_32 createFromSerialized(InputStream is) throws IOException {
54 return (Trie2_32) Trie2.createFromSerialized(is);
58 * Get the value for a code point as stored in the Trie2.
60 * @param codePoint the code point
64 public final int get(int codePoint) {
69 if (codePoint < 0x0d800 || (codePoint > 0x0dbff && codePoint <= 0x0ffff)) {
70 // Ordinary BMP code point, excluding leading surrogates.
71 // BMP uses a single level lookup. BMP index starts at offset 0 in the Trie2 index.
72 // 32 bit data is stored in the index array itself.
73 ix = index[codePoint >> UTRIE2_SHIFT_2];
74 ix = (ix << UTRIE2_INDEX_SHIFT) + (codePoint & UTRIE2_DATA_MASK);
78 if (codePoint <= 0xffff) {
79 // Lead Surrogate Code Point. A Separate index section is stored for
80 // lead surrogate code units and code points.
81 // The main index has the code unit data.
82 // For this function, we need the code point data.
83 // Note: this expression could be refactored for slightly improved efficiency, but
84 // surrogate code points will be so rare in practice that it's not worth it.
85 ix = index[UTRIE2_LSCP_INDEX_2_OFFSET + ((codePoint - 0xd800) >> UTRIE2_SHIFT_2)];
86 ix = (ix << UTRIE2_INDEX_SHIFT) + (codePoint & UTRIE2_DATA_MASK);
90 if (codePoint < highStart) {
91 // Supplemental code point, use two-level lookup.
92 ix = (UTRIE2_INDEX_1_OFFSET - UTRIE2_OMITTED_BMP_INDEX_1_LENGTH) + (codePoint >> UTRIE2_SHIFT_1);
94 ix += (codePoint >> UTRIE2_SHIFT_2) & UTRIE2_INDEX_2_MASK;
96 ix = (ix << UTRIE2_INDEX_SHIFT) + (codePoint & UTRIE2_DATA_MASK);
100 if (codePoint <= 0x10ffff) {
101 value = data32[highValueIndex];
106 // Fall through. The code point is outside of the legal range of 0..0x10ffff.
112 * Get a Trie2 value for a UTF-16 code unit.
114 * This function returns the same value as get() if the input
115 * character is outside of the lead surrogate range
117 * There are two values stored in a Trie2 for inputs in the lead
118 * surrogate range. This function returns the alternate value,
119 * while Trie2.get() returns the main value.
121 * @param codeUnit a 16 bit code unit or lead surrogate value.
125 public int getFromU16SingleLead(char codeUnit){
129 ix = index[codeUnit >> UTRIE2_SHIFT_2];
130 ix = (ix << UTRIE2_INDEX_SHIFT) + (codeUnit & UTRIE2_DATA_MASK);
137 * Serialize a Trie2_32 onto an OutputStream.
139 * A Trie2 can be serialized multiple times.
140 * The serialized data is compatible with ICU4C UTrie2 serialization.
141 * Trie2 serialization is unrelated to Java object serialization.
143 * @param os the stream to which the serialized Trie2 data will be written.
144 * @return the number of bytes written.
145 * @throw IOException on an error writing to the OutputStream.
147 public int serialize(OutputStream os) throws IOException {
148 DataOutputStream dos = new DataOutputStream(os);
149 int bytesWritten = 0;
151 bytesWritten += serializeHeader(dos);
152 for (int i=0; i<dataLength; i++) {
153 dos.writeInt(data32[i]);
155 bytesWritten += dataLength*4;
160 * @return the number of bytes of the serialized trie
162 public int getSerializedLength() {
163 return 16+header.indexLength*2+dataLength*4;
167 * Given a starting code point, find the last in a range of code points,
168 * all with the same value.
170 * This function is part of the implementation of iterating over the
172 * @param startingCP The code point at which to begin looking.
173 * @return The last code point with the same value as the starting code point.
176 int rangeEnd(int startingCP, int limit, int value) {
181 // Loop runs once for each of
182 // - a partial data block
183 // - a reference to the null (default) data block.
184 // - a reference to the index2 null block
191 if (cp < 0x0d800 || (cp > 0x0dbff && cp <= 0x0ffff)) {
192 // Ordinary BMP code point, excluding leading surrogates.
193 // BMP uses a single level lookup. BMP index starts at offset 0 in the Trie2 index.
194 // 16 bit data is stored in the index array itself.
196 block = index[cp >> UTRIE2_SHIFT_2] << UTRIE2_INDEX_SHIFT;
197 } else if (cp < 0xffff) {
198 // Lead Surrogate Code Point, 0xd800 <= cp < 0xdc00
199 index2Block = UTRIE2_LSCP_INDEX_2_OFFSET;
200 block = index[index2Block + ((cp - 0xd800) >> UTRIE2_SHIFT_2)] << UTRIE2_INDEX_SHIFT;
201 } else if (cp < highStart) {
202 // Supplemental code point, use two-level lookup.
203 int ix = (UTRIE2_INDEX_1_OFFSET - UTRIE2_OMITTED_BMP_INDEX_1_LENGTH) + (cp >> UTRIE2_SHIFT_1);
204 index2Block = index[ix];
205 block = index[index2Block + ((cp >> UTRIE2_SHIFT_2) & UTRIE2_INDEX_2_MASK)] << UTRIE2_INDEX_SHIFT;
207 // Code point above highStart.
208 if (value == data32[highValueIndex]) {
214 if (index2Block == index2NullOffset) {
215 if (value != initialValue) {
218 cp += UTRIE2_CP_PER_INDEX_1_ENTRY;
219 } else if (block == dataNullOffset) {
220 // The block at dataNullOffset has all values == initialValue.
221 // Because Trie2 iteration always proceeds in ascending order, we will always
222 // encounter a null block at its beginning, and can skip over
223 // a number of code points equal to the length of the block.
224 if (value != initialValue) {
227 cp += UTRIE2_DATA_BLOCK_LENGTH;
229 // Current position refers to an ordinary data block.
230 // Walk over the data entries, checking the values.
231 int startIx = block + (cp & UTRIE2_DATA_MASK);
232 int limitIx = block + UTRIE2_DATA_BLOCK_LENGTH;
233 for (int ix = startIx; ix<limitIx; ix++) {
234 if (data32[ix] != value) {
235 // We came to an entry with a different value.
237 cp += (ix - startIx);
241 // The ordinary data block contained our value until its end.
242 // Advance the current code point, and continue the outer loop.
243 cp += limitIx - startIx;