1 16 19 package org.apache.xml.utils; 20 21 42 public class SuballocatedByteVector 43 { 44 45 protected int m_blocksize; 46 47 48 protected int m_numblocks=32; 49 50 51 protected byte m_map[][]; 52 53 54 protected int m_firstFree = 0; 55 56 57 protected byte m_map0[]; 58 59 63 public SuballocatedByteVector() 64 { 65 this(2048); 66 } 67 68 73 public SuballocatedByteVector(int blocksize) 74 { 75 m_blocksize = blocksize; 76 m_map0=new byte[blocksize]; 77 m_map = new byte[m_numblocks][]; 78 m_map[0]=m_map0; 79 } 80 81 86 public SuballocatedByteVector(int blocksize, int increaseSize) 87 { 88 this(blocksize); 90 } 91 92 93 98 public int size() 99 { 100 return m_firstFree; 101 } 102 103 108 private void setSize(int sz) 109 { 110 if(m_firstFree<sz) 111 m_firstFree = sz; 112 } 113 114 119 public void addElement(byte value) 120 { 121 if(m_firstFree<m_blocksize) 122 m_map0[m_firstFree++]=value; 123 else 124 { 125 int index=m_firstFree/m_blocksize; 126 int offset=m_firstFree%m_blocksize; 127 ++m_firstFree; 128 129 if(index>=m_map.length) 130 { 131 int newsize=index+m_numblocks; 132 byte[][] newMap=new byte[newsize][]; 133 System.arraycopy(m_map, 0, newMap, 0, m_map.length); 134 m_map=newMap; 135 } 136 byte[] block=m_map[index]; 137 if(null==block) 138 block=m_map[index]=new byte[m_blocksize]; 139 block[offset]=value; 140 } 141 } 142 143 148 private void addElements(byte value, int numberOfElements) 149 { 150 if(m_firstFree+numberOfElements<m_blocksize) 151 for (int i = 0; i < numberOfElements; i++) 152 { 153 m_map0[m_firstFree++]=value; 154 } 155 else 156 { 157 int index=m_firstFree/m_blocksize; 158 int offset=m_firstFree%m_blocksize; 159 m_firstFree+=numberOfElements; 160 while( numberOfElements>0) 161 { 162 if(index>=m_map.length) 163 { 164 int newsize=index+m_numblocks; 165 byte[][] newMap=new byte[newsize][]; 166 System.arraycopy(m_map, 0, newMap, 0, m_map.length); 167 m_map=newMap; 168 } 169 byte[] block=m_map[index]; 170 if(null==block) 171 block=m_map[index]=new byte[m_blocksize]; 172 int copied=(m_blocksize-offset < numberOfElements) 173 ? m_blocksize-offset : numberOfElements; 174 numberOfElements-=copied; 175 while(copied-- > 0) 176 block[offset++]=value; 177 178 ++index;offset=0; 179 } 180 } 181 } 182 183 189 private void addElements(int numberOfElements) 190 { 191 int newlen=m_firstFree+numberOfElements; 192 if(newlen>m_blocksize) 193 { 194 int index=m_firstFree%m_blocksize; 195 int newindex=(m_firstFree+numberOfElements)%m_blocksize; 196 for(int i=index+1;i<=newindex;++i) 197 m_map[i]=new byte[m_blocksize]; 198 } 199 m_firstFree=newlen; 200 } 201 202 213 private void insertElementAt(byte value, int at) 214 { 215 if(at==m_firstFree) 216 addElement(value); 217 else if (at>m_firstFree) 218 { 219 int index=at/m_blocksize; 220 if(index>=m_map.length) 221 { 222 int newsize=index+m_numblocks; 223 byte[][] newMap=new byte[newsize][]; 224 System.arraycopy(m_map, 0, newMap, 0, m_map.length); 225 m_map=newMap; 226 } 227 byte[] block=m_map[index]; 228 if(null==block) 229 block=m_map[index]=new byte[m_blocksize]; 230 int offset=at%m_blocksize; 231 block[offset]=value; 232 m_firstFree=offset+1; 233 } 234 else 235 { 236 int index=at/m_blocksize; 237 int maxindex=m_firstFree+1/m_blocksize; 238 ++m_firstFree; 239 int offset=at%m_blocksize; 240 byte push; 241 242 while(index<=maxindex) 244 { 245 int copylen=m_blocksize-offset-1; 246 byte[] block=m_map[index]; 247 if(null==block) 248 { 249 push=0; 250 block=m_map[index]=new byte[m_blocksize]; 251 } 252 else 253 { 254 push=block[m_blocksize-1]; 255 System.arraycopy(block, offset , block, offset+1, copylen); 256 } 257 block[offset]=value; 258 value=push; 259 offset=0; 260 ++index; 261 } 262 } 263 } 264 265 268 public void removeAllElements() 269 { 270 m_firstFree = 0; 271 } 272 273 284 private boolean removeElement(byte s) 285 { 286 int at=indexOf(s,0); 287 if(at<0) 288 return false; 289 removeElementAt(at); 290 return true; 291 } 292 293 301 private void removeElementAt(int at) 302 { 303 if(at<m_firstFree) 305 { 306 int index=at/m_blocksize; 307 int maxindex=m_firstFree/m_blocksize; 308 int offset=at%m_blocksize; 309 310 while(index<=maxindex) 311 { 312 int copylen=m_blocksize-offset-1; 313 byte[] block=m_map[index]; 314 if(null==block) 315 block=m_map[index]=new byte[m_blocksize]; 316 else 317 System.arraycopy(block, offset+1, block, offset, copylen); 318 if(index<maxindex) 319 { 320 byte[] next=m_map[index+1]; 321 if(next!=null) 322 block[m_blocksize-1]=(next!=null) ? next[0] : 0; 323 } 324 else 325 block[m_blocksize-1]=0; 326 offset=0; 327 ++index; 328 } 329 } 330 --m_firstFree; 331 } 332 333 343 public void setElementAt(byte value, int at) 344 { 345 if(at<m_blocksize) 346 { 347 m_map0[at]=value; 348 return; 349 } 350 351 int index=at/m_blocksize; 352 int offset=at%m_blocksize; 353 354 if(index>=m_map.length) 355 { 356 int newsize=index+m_numblocks; 357 byte[][] newMap=new byte[newsize][]; 358 System.arraycopy(m_map, 0, newMap, 0, m_map.length); 359 m_map=newMap; 360 } 361 362 byte[] block=m_map[index]; 363 if(null==block) 364 block=m_map[index]=new byte[m_blocksize]; 365 block[offset]=value; 366 367 if(at>=m_firstFree) 368 m_firstFree=at+1; 369 } 370 371 392 public byte elementAt(int i) 393 { 394 if(i<m_blocksize) 397 return m_map0[i]; 398 399 return m_map[i/m_blocksize][i%m_blocksize]; 400 } 401 402 409 private boolean contains(byte s) 410 { 411 return (indexOf(s,0) >= 0); 412 } 413 414 425 public int indexOf(byte elem, int index) 426 { 427 if(index>=m_firstFree) 428 return -1; 429 430 int bindex=index/m_blocksize; 431 int boffset=index%m_blocksize; 432 int maxindex=m_firstFree/m_blocksize; 433 byte[] block; 434 435 for(;bindex<maxindex;++bindex) 436 { 437 block=m_map[bindex]; 438 if(block!=null) 439 for(int offset=boffset;offset<m_blocksize;++offset) 440 if(block[offset]==elem) 441 return offset+bindex*m_blocksize; 442 boffset=0; } 444 int maxoffset=m_firstFree%m_blocksize; 446 block=m_map[maxindex]; 447 for(int offset=boffset;offset<maxoffset;++offset) 448 if(block[offset]==elem) 449 return offset+maxindex*m_blocksize; 450 451 return -1; 452 } 453 454 464 public int indexOf(byte elem) 465 { 466 return indexOf(elem,0); 467 } 468 469 479 private int lastIndexOf(byte elem) 480 { 481 int boffset=m_firstFree%m_blocksize; 482 for(int index=m_firstFree/m_blocksize; 483 index>=0; 484 --index) 485 { 486 byte[] block=m_map[index]; 487 if(block!=null) 488 for(int offset=boffset; offset>=0; --offset) 489 if(block[offset]==elem) 490 return offset+index*m_blocksize; 491 boffset=0; } 493 return -1; 494 } 495 496 } 497 | Popular Tags |