1 7 8 package com.ibm.icu.impl; 9 10 import java.io.InputStream ; 11 import java.io.DataInputStream ; 12 import java.io.IOException ; 13 import java.util.Arrays ; 14 import com.ibm.icu.text.UTF16; 15 16 22 public class IntTrie extends Trie 23 { 24 26 37 public IntTrie(InputStream inputStream, DataManipulate dataManipulate) 38 throws IOException 39 { 40 super(inputStream, dataManipulate); 41 if (!isIntTrie()) { 42 throw new IllegalArgumentException ( 43 "Data given does not belong to a int trie."); 44 } 45 } 46 47 61 public IntTrie(int initialValue, int leadUnitValue, DataManipulate dataManipulate) { 62 super(new char[BMP_INDEX_LENGTH+SURROGATE_BLOCK_COUNT], HEADER_OPTIONS_LATIN1_IS_LINEAR_MASK_, dataManipulate); 63 64 int dataLength, latin1Length, i, limit; 65 char block; 66 67 68 69 70 dataLength=latin1Length= INDEX_STAGE_1_SHIFT_<=8 ? 256 : DATA_BLOCK_LENGTH; 71 if(leadUnitValue!=initialValue) { 72 dataLength+=DATA_BLOCK_LENGTH; 73 } 74 m_data_=new int[dataLength]; 75 m_dataLength_=dataLength; 76 77 m_initialValue_=initialValue; 78 79 80 81 82 83 84 for(i=0; i<latin1Length; ++i) { 85 m_data_[i]=initialValue; 86 } 87 88 if(leadUnitValue!=initialValue) { 89 90 block=(char)(latin1Length>>INDEX_STAGE_2_SHIFT_); 91 i=0xd800>>INDEX_STAGE_1_SHIFT_; 92 limit=0xdc00>>INDEX_STAGE_1_SHIFT_; 93 for(; i<limit; ++i) { 94 m_index_[i]=block; 95 } 96 97 98 limit=latin1Length+DATA_BLOCK_LENGTH; 99 for(i=latin1Length; i<limit; ++i) { 100 m_data_[i]=leadUnitValue; 101 } 102 } 103 } 104 105 107 115 public final int getCodePointValue(int ch) 116 { 117 int offset; 118 119 if(0 <= ch && ch < UTF16.LEAD_SURROGATE_MIN_VALUE) { 121 offset = (m_index_[ch >> INDEX_STAGE_1_SHIFT_] << INDEX_STAGE_2_SHIFT_) 123 + (ch & INDEX_STAGE_3_MASK_); 124 return m_data_[offset]; 125 } 126 127 offset = getCodePointOffset(ch); 129 return (offset >= 0) ? m_data_[offset] : m_initialValue_; 130 } 131 132 142 public final int getLeadValue(char ch) 143 { 144 return m_data_[getLeadOffset(ch)]; 145 } 146 147 155 public final int getBMPValue(char ch) 156 { 157 return m_data_[getBMPOffset(ch)]; 158 } 159 160 166 public final int getSurrogateValue(char lead, char trail) 167 { 168 if (!UTF16.isLeadSurrogate(lead) || !UTF16.isTrailSurrogate(trail)) { 169 throw new IllegalArgumentException ( 170 "Argument characters do not form a supplementary character"); 171 } 172 int offset = getSurrogateOffset(lead, trail); 174 175 if (offset > 0) { 177 return m_data_[offset]; 178 } 179 180 return m_initialValue_; 182 } 183 184 193 public final int getTrailValue(int leadvalue, char trail) 194 { 195 if (m_dataManipulate_ == null) { 196 throw new NullPointerException ( 197 "The field DataManipulate in this Trie is null"); 198 } 199 int offset = m_dataManipulate_.getFoldingOffset(leadvalue); 200 if (offset > 0) { 201 return m_data_[getRawOffset(offset, 202 (char)(trail & SURROGATE_MASK_))]; 203 } 204 return m_initialValue_; 205 } 206 207 214 public final int getLatin1LinearValue(char ch) 215 { 216 return m_data_[INDEX_STAGE_3_MASK_ + 1 + ch]; 217 } 218 219 225 public boolean equals(Object other) 227 { 228 boolean result = super.equals(other); 229 if (result && other instanceof IntTrie) { 230 IntTrie othertrie = (IntTrie)other; 231 if (m_initialValue_ != othertrie.m_initialValue_ 232 || !Arrays.equals(m_data_, othertrie.m_data_)) { 233 return false; 234 } 235 return true; 236 } 237 return false; 238 } 239 241 243 249 protected final void unserialize(InputStream inputStream) 250 throws IOException 251 { 252 super.unserialize(inputStream); 253 m_data_ = new int[m_dataLength_]; 255 DataInputStream input = new DataInputStream (inputStream); 256 for (int i = 0; i < m_dataLength_; i ++) { 257 m_data_[i] = input.readInt(); 258 } 259 m_initialValue_ = m_data_[0]; 260 } 261 262 269 protected final int getSurrogateOffset(char lead, char trail) 270 { 271 if (m_dataManipulate_ == null) { 272 throw new NullPointerException ( 273 "The field DataManipulate in this Trie is null"); 274 } 275 int offset = m_dataManipulate_.getFoldingOffset(getLeadValue(lead)); 277 278 if (offset > 0) { 280 return getRawOffset(offset, (char)(trail & SURROGATE_MASK_)); 281 } 282 283 return -1; 286 } 287 288 296 protected final int getValue(int index) 297 { 298 return m_data_[index]; 299 } 300 301 306 protected final int getInitialValue() 307 { 308 return m_initialValue_; 309 } 310 311 313 321 IntTrie(char index[], int data[], int initialvalue, int options, 322 DataManipulate datamanipulate) 323 { 324 super(index, options, datamanipulate); 325 m_data_ = data; 326 m_dataLength_ = m_data_.length; 327 m_initialValue_ = initialvalue; 328 } 329 330 332 335 private int m_initialValue_; 336 339 private int m_data_[]; 340 } 341 | Popular Tags |