1 16 19 package com.sun.org.apache.xml.internal.utils; 20 21 41 public class SuballocatedIntVector 42 { 43 44 protected int m_blocksize; 45 46 47 protected int m_SHIFT, m_MASK; 48 49 50 protected static final int NUMBLOCKS_DEFAULT = 32; 51 52 53 protected int m_numblocks = NUMBLOCKS_DEFAULT; 54 55 56 protected int m_map[][]; 57 58 59 protected int m_firstFree = 0; 60 61 62 protected int m_map0[]; 63 64 68 protected int m_buildCache[]; 69 protected int m_buildCacheStartIndex; 70 71 72 77 public SuballocatedIntVector() 78 { 79 this(2048); 80 } 81 82 90 public SuballocatedIntVector(int blocksize, int numblocks) 91 { 92 for(m_SHIFT=0;0!=(blocksize>>>=1);++m_SHIFT) 94 ; 95 m_blocksize=1<<m_SHIFT; 96 m_MASK=m_blocksize-1; 97 m_numblocks = numblocks; 98 99 m_map0=new int[m_blocksize]; 100 m_map = new int[numblocks][]; 101 m_map[0]=m_map0; 102 m_buildCache = m_map0; 103 m_buildCacheStartIndex = 0; 104 } 105 106 111 public SuballocatedIntVector(int blocksize) 112 { 113 this(blocksize, NUMBLOCKS_DEFAULT); 114 } 115 116 121 public int size() 122 { 123 return m_firstFree; 124 } 125 126 132 public void setSize(int sz) 133 { 134 if(m_firstFree>sz) m_firstFree = sz; 136 } 137 138 143 public void addElement(int value) 144 { 145 int indexRelativeToCache = m_firstFree - m_buildCacheStartIndex; 146 147 if(indexRelativeToCache >= 0 && indexRelativeToCache < m_blocksize) { 149 m_buildCache[indexRelativeToCache]=value; 150 ++m_firstFree; 151 } else { 152 159 int index=m_firstFree>>>m_SHIFT; 160 int offset=m_firstFree&m_MASK; 161 162 if(index>=m_map.length) 163 { 164 int newsize=index+m_numblocks; 165 int[][] newMap=new int[newsize][]; 166 System.arraycopy(m_map, 0, newMap, 0, m_map.length); 167 m_map=newMap; 168 } 169 int[] block=m_map[index]; 170 if(null==block) 171 block=m_map[index]=new int[m_blocksize]; 172 block[offset]=value; 173 174 m_buildCache = block; 177 m_buildCacheStartIndex = m_firstFree-offset; 178 179 ++m_firstFree; 180 } 181 } 182 183 188 private void addElements(int value, int numberOfElements) 189 { 190 if(m_firstFree+numberOfElements<m_blocksize) 191 for (int i = 0; i < numberOfElements; i++) 192 { 193 m_map0[m_firstFree++]=value; 194 } 195 else 196 { 197 int index=m_firstFree>>>m_SHIFT; 198 int offset=m_firstFree&m_MASK; 199 m_firstFree+=numberOfElements; 200 while( numberOfElements>0) 201 { 202 if(index>=m_map.length) 203 { 204 int newsize=index+m_numblocks; 205 int[][] newMap=new int[newsize][]; 206 System.arraycopy(m_map, 0, newMap, 0, m_map.length); 207 m_map=newMap; 208 } 209 int[] block=m_map[index]; 210 if(null==block) 211 block=m_map[index]=new int[m_blocksize]; 212 int copied=(m_blocksize-offset < numberOfElements) 213 ? m_blocksize-offset : numberOfElements; 214 numberOfElements-=copied; 215 while(copied-- > 0) 216 block[offset++]=value; 217 218 ++index;offset=0; 219 } 220 } 221 } 222 223 229 private void addElements(int numberOfElements) 230 { 231 int newlen=m_firstFree+numberOfElements; 232 if(newlen>m_blocksize) 233 { 234 int index=m_firstFree>>>m_SHIFT; 235 int newindex=(m_firstFree+numberOfElements)>>>m_SHIFT; 236 for(int i=index+1;i<=newindex;++i) 237 m_map[i]=new int[m_blocksize]; 238 } 239 m_firstFree=newlen; 240 } 241 242 253 private void insertElementAt(int value, int at) 254 { 255 if(at==m_firstFree) 256 addElement(value); 257 else if (at>m_firstFree) 258 { 259 int index=at>>>m_SHIFT; 260 if(index>=m_map.length) 261 { 262 int newsize=index+m_numblocks; 263 int[][] newMap=new int[newsize][]; 264 System.arraycopy(m_map, 0, newMap, 0, m_map.length); 265 m_map=newMap; 266 } 267 int[] block=m_map[index]; 268 if(null==block) 269 block=m_map[index]=new int[m_blocksize]; 270 int offset=at&m_MASK; 271 block[offset]=value; 272 m_firstFree=offset+1; 273 } 274 else 275 { 276 int index=at>>>m_SHIFT; 277 int maxindex=m_firstFree>>>m_SHIFT; ++m_firstFree; 279 int offset=at&m_MASK; 280 int push; 281 282 while(index<=maxindex) 284 { 285 int copylen=m_blocksize-offset-1; 286 int[] block=m_map[index]; 287 if(null==block) 288 { 289 push=0; 290 block=m_map[index]=new int[m_blocksize]; 291 } 292 else 293 { 294 push=block[m_blocksize-1]; 295 System.arraycopy(block, offset , block, offset+1, copylen); 296 } 297 block[offset]=value; 298 value=push; 299 offset=0; 300 ++index; 301 } 302 } 303 } 304 305 308 public void removeAllElements() 309 { 310 m_firstFree = 0; 311 m_buildCache = m_map0; 312 m_buildCacheStartIndex = 0; 313 } 314 315 326 private boolean removeElement(int s) 327 { 328 int at=indexOf(s,0); 329 if(at<0) 330 return false; 331 removeElementAt(at); 332 return true; 333 } 334 335 343 private void removeElementAt(int at) 344 { 345 if(at<m_firstFree) 347 { 348 int index=at>>>m_SHIFT; 349 int maxindex=m_firstFree>>>m_SHIFT; 350 int offset=at&m_MASK; 351 352 while(index<=maxindex) 353 { 354 int copylen=m_blocksize-offset-1; 355 int[] block=m_map[index]; 356 if(null==block) 357 block=m_map[index]=new int[m_blocksize]; 358 else 359 System.arraycopy(block, offset+1, block, offset, copylen); 360 if(index<maxindex) 361 { 362 int[] next=m_map[index+1]; 363 if(next!=null) 364 block[m_blocksize-1]=(next!=null) ? next[0] : 0; 365 } 366 else 367 block[m_blocksize-1]=0; 368 offset=0; 369 ++index; 370 } 371 } 372 --m_firstFree; 373 } 374 375 385 public void setElementAt(int value, int at) 386 { 387 if(at<m_blocksize) 388 m_map0[at]=value; 389 else 390 { 391 int index=at>>>m_SHIFT; 392 int offset=at&m_MASK; 393 394 if(index>=m_map.length) 395 { 396 int newsize=index+m_numblocks; 397 int[][] newMap=new int[newsize][]; 398 System.arraycopy(m_map, 0, newMap, 0, m_map.length); 399 m_map=newMap; 400 } 401 402 int[] block=m_map[index]; 403 if(null==block) 404 block=m_map[index]=new int[m_blocksize]; 405 block[offset]=value; 406 } 407 408 if(at>=m_firstFree) 409 m_firstFree=at+1; 410 } 411 412 413 434 public int elementAt(int i) 435 { 436 if(i<m_blocksize) 438 return m_map0[i]; 439 440 return m_map[i>>>m_SHIFT][i&m_MASK]; 441 } 442 443 450 private boolean contains(int s) 451 { 452 return (indexOf(s,0) >= 0); 453 } 454 455 466 public int indexOf(int elem, int index) 467 { 468 if(index>=m_firstFree) 469 return -1; 470 471 int bindex=index>>>m_SHIFT; 472 int boffset=index&m_MASK; 473 int maxindex=m_firstFree>>>m_SHIFT; 474 int[] block; 475 476 for(;bindex<maxindex;++bindex) 477 { 478 block=m_map[bindex]; 479 if(block!=null) 480 for(int offset=boffset;offset<m_blocksize;++offset) 481 if(block[offset]==elem) 482 return offset+bindex*m_blocksize; 483 boffset=0; } 485 int maxoffset=m_firstFree&m_MASK; 487 block=m_map[maxindex]; 488 for(int offset=boffset;offset<maxoffset;++offset) 489 if(block[offset]==elem) 490 return offset+maxindex*m_blocksize; 491 492 return -1; 493 } 494 495 505 public int indexOf(int elem) 506 { 507 return indexOf(elem,0); 508 } 509 510 520 private int lastIndexOf(int elem) 521 { 522 int boffset=m_firstFree&m_MASK; 523 for(int index=m_firstFree>>>m_SHIFT; 524 index>=0; 525 --index) 526 { 527 int[] block=m_map[index]; 528 if(block!=null) 529 for(int offset=boffset; offset>=0; --offset) 530 if(block[offset]==elem) 531 return offset+index*m_blocksize; 532 boffset=0; } 534 return -1; 535 } 536 537 541 public final int[] getMap0() 542 { 543 return m_map0; 544 } 545 546 550 public final int[][] getMap() 551 { 552 return m_map; 553 } 554 555 } 556 | Popular Tags |