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 14 import com.ibm.icu.text.UTF16; 15 16 22 23 public class CharTrie extends Trie 26 { 27 29 40 public CharTrie(InputStream inputStream, 41 DataManipulate dataManipulate) throws IOException 42 { 43 super(inputStream, dataManipulate); 44 45 if (!isCharTrie()) { 46 throw new IllegalArgumentException ( 47 "Data given does not belong to a char trie."); 48 } 49 m_friendAgent_ = new FriendAgent(); 50 } 51 52 66 public CharTrie(int initialValue, int leadUnitValue, DataManipulate dataManipulate) { 67 super(new char[BMP_INDEX_LENGTH+SURROGATE_BLOCK_COUNT], HEADER_OPTIONS_LATIN1_IS_LINEAR_MASK_, dataManipulate); 68 69 int dataLength, latin1Length, i, limit; 70 char block; 71 72 73 74 75 dataLength=latin1Length= INDEX_STAGE_1_SHIFT_<=8 ? 256 : DATA_BLOCK_LENGTH; 76 if(leadUnitValue!=initialValue) { 77 dataLength+=DATA_BLOCK_LENGTH; 78 } 79 m_data_=new char[dataLength]; 80 m_dataLength_=dataLength; 81 82 m_initialValue_=(char)initialValue; 83 84 85 86 87 88 89 for(i=0; i<latin1Length; ++i) { 90 m_data_[i]=(char)initialValue; 91 } 92 93 if(leadUnitValue!=initialValue) { 94 95 block=(char)(latin1Length>>INDEX_STAGE_2_SHIFT_); 96 i=0xd800>>INDEX_STAGE_1_SHIFT_; 97 limit=0xdc00>>INDEX_STAGE_1_SHIFT_; 98 for(; i<limit; ++i) { 99 m_index_[i]=block; 100 } 101 102 103 limit=latin1Length+DATA_BLOCK_LENGTH; 104 for(i=latin1Length; i<limit; ++i) { 105 m_data_[i]=(char)leadUnitValue; 106 } 107 } 108 109 m_friendAgent_ = new FriendAgent(); 110 } 111 112 115 public class FriendAgent 116 { 117 121 public char[] getPrivateIndex() 122 { 123 return m_index_; 124 } 125 129 public char[] getPrivateData() 130 { 131 return m_data_; 132 } 133 137 public int getPrivateInitialValue() 138 { 139 return m_initialValue_; 140 } 141 } 142 143 145 150 public void putIndexData(UCharacterProperty friend) 151 { 152 friend.setIndexData(m_friendAgent_); 153 } 154 155 163 public final char getCodePointValue(int ch) 164 { 165 int offset; 166 167 if(0 <= ch && ch < UTF16.LEAD_SURROGATE_MIN_VALUE) { 169 offset = (m_index_[ch >> INDEX_STAGE_1_SHIFT_] << INDEX_STAGE_2_SHIFT_) 171 + (ch & INDEX_STAGE_3_MASK_); 172 return m_data_[offset]; 173 } 174 175 offset = getCodePointOffset(ch); 177 178 return (offset >= 0) ? m_data_[offset] : m_initialValue_; 181 } 182 183 193 public final char getLeadValue(char ch) 194 { 195 return m_data_[getLeadOffset(ch)]; 196 } 197 198 206 public final char getBMPValue(char ch) 207 { 208 return m_data_[getBMPOffset(ch)]; 209 } 210 211 217 public final char getSurrogateValue(char lead, char trail) 218 { 219 int offset = getSurrogateOffset(lead, trail); 220 if (offset > 0) { 221 return m_data_[offset]; 222 } 223 return m_initialValue_; 224 } 225 226 236 public final char getTrailValue(int leadvalue, char trail) 237 { 238 if (m_dataManipulate_ == null) { 239 throw new NullPointerException ( 240 "The field DataManipulate in this Trie is null"); 241 } 242 int offset = m_dataManipulate_.getFoldingOffset(leadvalue); 243 if (offset > 0) { 244 return m_data_[getRawOffset(offset, 245 (char)(trail & SURROGATE_MASK_))]; 246 } 247 return m_initialValue_; 248 } 249 250 257 public final char getLatin1LinearValue(char ch) 258 { 259 return m_data_[INDEX_STAGE_3_MASK_ + 1 + m_dataOffset_ + ch]; 260 } 261 262 268 public boolean equals(Object other) 270 { 271 boolean result = super.equals(other); 272 if (result && other instanceof CharTrie) { 273 CharTrie othertrie = (CharTrie)other; 274 return m_initialValue_ == othertrie.m_initialValue_; 275 } 276 return false; 277 } 278 280 282 288 protected final void unserialize(InputStream inputStream) 289 throws IOException 290 { 291 DataInputStream input = new DataInputStream (inputStream); 292 int indexDataLength = m_dataOffset_ + m_dataLength_; 293 m_index_ = new char[indexDataLength]; 294 for (int i = 0; i < indexDataLength; i ++) { 295 m_index_[i] = input.readChar(); 296 } 297 m_data_ = m_index_; 298 m_initialValue_ = m_data_[m_dataOffset_]; 299 } 300 301 308 protected final int getSurrogateOffset(char lead, char trail) 309 { 310 if (m_dataManipulate_ == null) { 311 throw new NullPointerException ( 312 "The field DataManipulate in this Trie is null"); 313 } 314 315 int offset = m_dataManipulate_.getFoldingOffset(getLeadValue(lead)); 317 318 if (offset > 0) { 320 return getRawOffset(offset, (char)(trail & SURROGATE_MASK_)); 321 } 322 323 return -1; 326 } 327 328 336 protected final int getValue(int index) 337 { 338 return m_data_[index]; 339 } 340 341 346 protected final int getInitialValue() 347 { 348 return m_initialValue_; 349 } 350 351 353 356 private char m_initialValue_; 357 360 private char m_data_[]; 361 364 private FriendAgent m_friendAgent_; 365 } 366 | Popular Tags |