1 16 19 package org.apache.xml.dtm.ref; 20 21 import org.apache.xml.res.XMLErrorResources; 22 import org.apache.xml.res.XMLMessages; 23 24 38 final class ChunkedIntArray 39 { 40 final int slotsize=4; static final int lowbits=10; static final int chunkalloc=1<<lowbits; 45 static final int lowmask=chunkalloc-1; 46 47 ChunksVector chunks=new ChunksVector(); 48 final int fastArray[] = new int[chunkalloc]; 49 int lastUsed=0; 50 51 55 ChunkedIntArray(int slotsize) 56 { 57 if(this.slotsize<slotsize) 58 throw new ArrayIndexOutOfBoundsException (XMLMessages.createXMLMessage(XMLErrorResources.ER_CHUNKEDINTARRAY_NOT_SUPPORTED, new Object []{Integer.toString(slotsize)})); else if (this.slotsize>slotsize) 60 System.out.println("*****WARNING: ChunkedIntArray("+slotsize+") wasting "+(this.slotsize-slotsize)+" words per slot"); 61 chunks.addElement(fastArray); 62 } 63 69 int appendSlot(int w0, int w1, int w2, int w3) 70 { 71 83 { 84 final int slotsize=4; 85 int newoffset = (lastUsed+1)*slotsize; 86 int chunkpos = newoffset >> lowbits; 87 int slotpos = (newoffset & lowmask); 88 89 if (chunkpos > chunks.size() - 1) 91 chunks.addElement(new int[chunkalloc]); 92 int[] chunk = chunks.elementAt(chunkpos); 93 chunk[slotpos] = w0; 94 chunk[slotpos+1] = w1; 95 chunk[slotpos+2] = w2; 96 chunk[slotpos+3] = w3; 97 98 return ++lastUsed; 99 } 100 } 101 108 int readEntry(int position, int offset) throws ArrayIndexOutOfBoundsException 109 { 110 117 { 118 if (offset>=slotsize) 120 throw new ArrayIndexOutOfBoundsException (XMLMessages.createXMLMessage(XMLErrorResources.ER_OFFSET_BIGGER_THAN_SLOT, null)); position*=slotsize; 122 int chunkpos = position >> lowbits; 123 int slotpos = position & lowmask; 124 int[] chunk = chunks.elementAt(chunkpos); 125 return chunk[slotpos + offset]; 126 } 127 } 128 129 int specialFind(int startPos, int position) 136 { 137 int ancestor = startPos; 140 while(ancestor > 0) 141 { 142 ancestor*=slotsize; 144 int chunkpos = ancestor >> lowbits; 145 int slotpos = ancestor & lowmask; 146 int[] chunk = chunks.elementAt(chunkpos); 147 148 ancestor = chunk[slotpos + 1]; 152 153 if(ancestor == position) 154 break; 155 } 156 157 if (ancestor <= 0) 158 { 159 return position; 160 } 161 return -1; 162 } 163 164 167 int slotsUsed() 168 { 169 return lastUsed; 170 } 171 172 177 void discardLast() 178 { 179 --lastUsed; 180 } 181 182 190 void writeEntry(int position, int offset, int value) throws ArrayIndexOutOfBoundsException 191 { 192 199 { 200 if (offset >= slotsize) 201 throw new ArrayIndexOutOfBoundsException (XMLMessages.createXMLMessage(XMLErrorResources.ER_OFFSET_BIGGER_THAN_SLOT, null)); position*=slotsize; 203 int chunkpos = position >> lowbits; 204 int slotpos = position & lowmask; 205 int[] chunk = chunks.elementAt(chunkpos); 206 chunk[slotpos + offset] = value; } 208 } 209 210 219 void writeSlot(int position, int w0, int w1, int w2, int w3) 220 { 221 position *= slotsize; 222 int chunkpos = position >> lowbits; 223 int slotpos = (position & lowmask); 224 225 if (chunkpos > chunks.size() - 1) 227 chunks.addElement(new int[chunkalloc]); 228 int[] chunk = chunks.elementAt(chunkpos); 229 chunk[slotpos] = w0; 230 chunk[slotpos + 1] = w1; 231 chunk[slotpos + 2] = w2; 232 chunk[slotpos + 3] = w3; 233 } 234 235 243 void readSlot(int position, int[] buffer) 244 { 245 252 { 253 position *= slotsize; 255 int chunkpos = position >> lowbits; 256 int slotpos = (position & lowmask); 257 258 if (chunkpos > chunks.size() - 1) 260 chunks.addElement(new int[chunkalloc]); 261 int[] chunk = chunks.elementAt(chunkpos); 262 System.arraycopy(chunk,slotpos,buffer,0,slotsize); 263 } 264 } 265 266 class ChunksVector 267 { 268 final int BLOCKSIZE = 64; 269 int[] m_map[] = new int[BLOCKSIZE][]; 270 int m_mapSize = BLOCKSIZE; 271 int pos = 0; 272 273 ChunksVector() 274 { 275 } 276 277 final int size() 278 { 279 return pos; 280 } 281 282 void addElement(int[] value) 283 { 284 if(pos >= m_mapSize) 285 { 286 int orgMapSize = m_mapSize; 287 while(pos >= m_mapSize) 288 m_mapSize+=BLOCKSIZE; 289 int[] newMap[] = new int[m_mapSize][]; 290 System.arraycopy(m_map, 0, newMap, 0, orgMapSize); 291 m_map = newMap; 292 } 293 m_map[pos] = value; 296 pos++; 297 } 298 299 final int[] elementAt(int pos) 300 { 301 return m_map[pos]; 302 } 303 } 304 } 305 | Popular Tags |