1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 /*
6  * Licensed to the Apache Software Foundation (ASF) under one or more
7  * contributor license agreements.  See the NOTICE file distributed with
8  * this work for additional information regarding copyright ownership.
9  * The ASF licenses this file to You under the Apache License, Version 2.0
10  * (the "License"); you may not use this file except in compliance with
11  * the License.  You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 package com.sun.org.apache.xml.internal.utils;
23 
24 /**
25  * A very simple table that stores a list of byte. Very similar API to our
26  * IntVector class (same API); different internal storage.
27  *
28  * This version uses an array-of-arrays solution. Read/write access is thus
29  * a bit slower than the simple IntVector, and basic storage is a trifle
30  * higher due to the top-level array -- but appending is O(1) fast rather
31  * than O(N**2) slow, which will swamp those costs in situations where
32  * long vectors are being built up.
33  *
34  * Known issues:
35  *
36  * Some methods are private because they haven't yet been tested properly.
37  *
38  * If an element has not been set (because we skipped it), its value will
39  * initially be 0. Shortening the vector does not clear old storage; if you
40  * then skip values and setElementAt a higher index again, you may see old data
41  * reappear in the truncated-and-restored section. Doing anything else would
42  * have performance costs.
43  * @xsl.usage internal
44  */
45 public class SuballocatedByteVector
46 {
47   /** Size of blocks to allocate          */
48   protected int m_blocksize;
49 
50   /** Number of blocks to (over)allocate by */
51   protected  int m_numblocks=32;
52 
53   /** Array of arrays of bytes          */
54   protected byte m_map[][];
55 
56   /** Number of bytes in array          */
57   protected int m_firstFree = 0;
58 
59   /** "Shortcut" handle to m_map[0] */
60   protected byte m_map0[];
61 
62   /**
63    * Default constructor.  Note that the default
64    * block size is very small, for small lists.
65    */
SuballocatedByteVector()66   public SuballocatedByteVector()
67   {
68     this(2048);
69   }
70 
71   /**
72    * Construct a ByteVector, using the given block size.
73    *
74    * @param blocksize Size of block to allocate
75    */
SuballocatedByteVector(int blocksize)76   public SuballocatedByteVector(int blocksize)
77   {
78     m_blocksize = blocksize;
79     m_map0=new byte[blocksize];
80     m_map = new byte[m_numblocks][];
81     m_map[0]=m_map0;
82   }
83 
84   /**
85    * Construct a ByteVector, using the given block size.
86    *
87    * @param blocksize Size of block to allocate
88    */
SuballocatedByteVector(int blocksize, int increaseSize)89   public SuballocatedByteVector(int blocksize, int increaseSize)
90   {
91     // increaseSize not currently used.
92     this(blocksize);
93   }
94 
95 
96   /**
97    * Get the length of the list.
98    *
99    * @return length of the list
100    */
size()101   public int size()
102   {
103     return m_firstFree;
104   }
105 
106   /**
107    * Set the length of the list.
108    *
109    * @return length of the list
110    */
setSize(int sz)111   private  void setSize(int sz)
112   {
113     if(m_firstFree<sz)
114       m_firstFree = sz;
115   }
116 
117   /**
118    * Append a byte onto the vector.
119    *
120    * @param value Byte to add to the list
121    */
addElement(byte value)122   public  void addElement(byte value)
123   {
124     if(m_firstFree<m_blocksize)
125       m_map0[m_firstFree++]=value;
126     else
127     {
128       int index=m_firstFree/m_blocksize;
129       int offset=m_firstFree%m_blocksize;
130       ++m_firstFree;
131 
132       if(index>=m_map.length)
133       {
134         int newsize=index+m_numblocks;
135         byte[][] newMap=new byte[newsize][];
136         System.arraycopy(m_map, 0, newMap, 0, m_map.length);
137         m_map=newMap;
138       }
139       byte[] block=m_map[index];
140       if(null==block)
141         block=m_map[index]=new byte[m_blocksize];
142       block[offset]=value;
143     }
144   }
145 
146   /**
147    * Append several byte values onto the vector.
148    *
149    * @param value Byte to add to the list
150    */
addElements(byte value, int numberOfElements)151   private  void addElements(byte value, int numberOfElements)
152   {
153     if(m_firstFree+numberOfElements<m_blocksize)
154       for (int i = 0; i < numberOfElements; i++)
155       {
156         m_map0[m_firstFree++]=value;
157       }
158     else
159     {
160       int index=m_firstFree/m_blocksize;
161       int offset=m_firstFree%m_blocksize;
162       m_firstFree+=numberOfElements;
163       while( numberOfElements>0)
164       {
165         if(index>=m_map.length)
166         {
167           int newsize=index+m_numblocks;
168           byte[][] newMap=new byte[newsize][];
169           System.arraycopy(m_map, 0, newMap, 0, m_map.length);
170           m_map=newMap;
171         }
172         byte[] block=m_map[index];
173         if(null==block)
174           block=m_map[index]=new byte[m_blocksize];
175         int copied=(m_blocksize-offset < numberOfElements)
176           ? m_blocksize-offset : numberOfElements;
177         numberOfElements-=copied;
178         while(copied-- > 0)
179           block[offset++]=value;
180 
181         ++index;offset=0;
182       }
183     }
184   }
185 
186   /**
187    * Append several slots onto the vector, but do not set the values.
188    * Note: "Not Set" means the value is unspecified.
189    *
190    * @param numberOfElements
191    */
addElements(int numberOfElements)192   private  void addElements(int numberOfElements)
193   {
194     int newlen=m_firstFree+numberOfElements;
195     if(newlen>m_blocksize)
196     {
197       int index=m_firstFree%m_blocksize;
198       int newindex=(m_firstFree+numberOfElements)%m_blocksize;
199       for(int i=index+1;i<=newindex;++i)
200         m_map[i]=new byte[m_blocksize];
201     }
202     m_firstFree=newlen;
203   }
204 
205   /**
206    * Inserts the specified node in this vector at the specified index.
207    * Each component in this vector with an index greater or equal to
208    * the specified index is shifted upward to have an index one greater
209    * than the value it had previously.
210    *
211    * Insertion may be an EXPENSIVE operation!
212    *
213    * @param value Byte to insert
214    * @param at Index of where to insert
215    */
insertElementAt(byte value, int at)216   private  void insertElementAt(byte value, int at)
217   {
218     if(at==m_firstFree)
219       addElement(value);
220     else if (at>m_firstFree)
221     {
222       int index=at/m_blocksize;
223       if(index>=m_map.length)
224       {
225         int newsize=index+m_numblocks;
226         byte[][] newMap=new byte[newsize][];
227         System.arraycopy(m_map, 0, newMap, 0, m_map.length);
228         m_map=newMap;
229       }
230       byte[] block=m_map[index];
231       if(null==block)
232         block=m_map[index]=new byte[m_blocksize];
233       int offset=at%m_blocksize;
234       block[offset]=value;
235       m_firstFree=offset+1;
236     }
237     else
238     {
239       int index=at/m_blocksize;
240       int maxindex=m_firstFree+1/m_blocksize;
241       ++m_firstFree;
242       int offset=at%m_blocksize;
243       byte push;
244 
245       // ***** Easier to work down from top?
246       while(index<=maxindex)
247       {
248         int copylen=m_blocksize-offset-1;
249         byte[] block=m_map[index];
250         if(null==block)
251         {
252           push=0;
253           block=m_map[index]=new byte[m_blocksize];
254         }
255         else
256         {
257           push=block[m_blocksize-1];
258           System.arraycopy(block, offset , block, offset+1, copylen);
259         }
260         block[offset]=value;
261         value=push;
262         offset=0;
263         ++index;
264       }
265     }
266   }
267 
268   /**
269    * Wipe it out.
270    */
removeAllElements()271   public void removeAllElements()
272   {
273     m_firstFree = 0;
274   }
275 
276   /**
277    * Removes the first occurrence of the argument from this vector.
278    * If the object is found in this vector, each component in the vector
279    * with an index greater or equal to the object's index is shifted
280    * downward to have an index one smaller than the value it had
281    * previously.
282    *
283    * @param s Byte to remove from array
284    *
285    * @return True if the byte was removed, false if it was not found
286    */
removeElement(byte s)287   private  boolean removeElement(byte s)
288   {
289     int at=indexOf(s,0);
290     if(at<0)
291       return false;
292     removeElementAt(at);
293     return true;
294   }
295 
296   /**
297    * Deletes the component at the specified index. Each component in
298    * this vector with an index greater or equal to the specified
299    * index is shifted downward to have an index one smaller than
300    * the value it had previously.
301    *
302    * @param at index of where to remove a byte
303    */
removeElementAt(int at)304   private  void removeElementAt(int at)
305   {
306     // No point in removing elements that "don't exist"...
307     if(at<m_firstFree)
308     {
309       int index=at/m_blocksize;
310       int maxindex=m_firstFree/m_blocksize;
311       int offset=at%m_blocksize;
312 
313       while(index<=maxindex)
314       {
315         int copylen=m_blocksize-offset-1;
316         byte[] block=m_map[index];
317         if(null==block)
318           block=m_map[index]=new byte[m_blocksize];
319         else
320           System.arraycopy(block, offset+1, block, offset, copylen);
321         if(index<maxindex)
322         {
323           byte[] next=m_map[index+1];
324           if(next!=null)
325             block[m_blocksize-1]=(next!=null) ? next[0] : 0;
326         }
327         else
328           block[m_blocksize-1]=0;
329         offset=0;
330         ++index;
331       }
332     }
333     --m_firstFree;
334   }
335 
336   /**
337    * Sets the component at the specified index of this vector to be the
338    * specified object. The previous component at that position is discarded.
339    *
340    * The index must be a value greater than or equal to 0 and less
341    * than the current size of the vector.
342    *
343    * @param value
344    * @param at     Index of where to set the object
345    */
setElementAt(byte value, int at)346   public void setElementAt(byte value, int at)
347   {
348     if(at<m_blocksize)
349     {
350       m_map0[at]=value;
351       return;
352     }
353 
354     int index=at/m_blocksize;
355     int offset=at%m_blocksize;
356 
357     if(index>=m_map.length)
358     {
359       int newsize=index+m_numblocks;
360       byte[][] newMap=new byte[newsize][];
361       System.arraycopy(m_map, 0, newMap, 0, m_map.length);
362       m_map=newMap;
363     }
364 
365     byte[] block=m_map[index];
366     if(null==block)
367       block=m_map[index]=new byte[m_blocksize];
368     block[offset]=value;
369 
370     if(at>=m_firstFree)
371       m_firstFree=at+1;
372   }
373 
374   /**
375    * Get the nth element. This is often at the innermost loop of an
376    * application, so performance is critical.
377    *
378    * @param i index of value to get
379    *
380    * @return value at given index. If that value wasn't previously set,
381    * the result is undefined for performance reasons. It may throw an
382    * exception (see below), may return zero, or (if setSize has previously
383    * been used) may return stale data.
384    *
385    * @throws ArrayIndexOutOfBoundsException if the index was _clearly_
386    * unreasonable (negative, or past the highest block).
387    *
388    * @throws NullPointerException if the index points to a block that could
389    * have existed (based on the highest index used) but has never had anything
390    * set into it.
391    * %REVIEW% Could add a catch to create the block in that case, or return 0.
392    * Try/Catch is _supposed_ to be nearly free when not thrown to. Do we
393    * believe that? Should we have a separate safeElementAt?
394    */
elementAt(int i)395   public byte elementAt(int i)
396   {
397     // %OPT% Does this really buy us anything? Test versus division for small,
398     // test _plus_ division for big docs.
399     if(i<m_blocksize)
400       return m_map0[i];
401 
402     return m_map[i/m_blocksize][i%m_blocksize];
403   }
404 
405   /**
406    * Tell if the table contains the given node.
407    *
408    * @param s object to look for
409    *
410    * @return true if the object is in the list
411    */
contains(byte s)412   private  boolean contains(byte s)
413   {
414     return (indexOf(s,0) >= 0);
415   }
416 
417   /**
418    * Searches for the first occurence of the given argument,
419    * beginning the search at index, and testing for equality
420    * using the equals method.
421    *
422    * @param elem object to look for
423    * @param index Index of where to begin search
424    * @return the index of the first occurrence of the object
425    * argument in this vector at position index or later in the
426    * vector; returns -1 if the object is not found.
427    */
indexOf(byte elem, int index)428   public int indexOf(byte elem, int index)
429   {
430     if(index>=m_firstFree)
431       return -1;
432 
433     int bindex=index/m_blocksize;
434     int boffset=index%m_blocksize;
435     int maxindex=m_firstFree/m_blocksize;
436     byte[] block;
437 
438     for(;bindex<maxindex;++bindex)
439     {
440       block=m_map[bindex];
441       if(block!=null)
442         for(int offset=boffset;offset<m_blocksize;++offset)
443           if(block[offset]==elem)
444             return offset+bindex*m_blocksize;
445       boffset=0; // after first
446     }
447     // Last block may need to stop before end
448     int maxoffset=m_firstFree%m_blocksize;
449     block=m_map[maxindex];
450     for(int offset=boffset;offset<maxoffset;++offset)
451       if(block[offset]==elem)
452         return offset+maxindex*m_blocksize;
453 
454     return -1;
455   }
456 
457   /**
458    * Searches for the first occurence of the given argument,
459    * beginning the search at index, and testing for equality
460    * using the equals method.
461    *
462    * @param elem object to look for
463    * @return the index of the first occurrence of the object
464    * argument in this vector at position index or later in the
465    * vector; returns -1 if the object is not found.
466    */
indexOf(byte elem)467   public int indexOf(byte elem)
468   {
469     return indexOf(elem,0);
470   }
471 
472   /**
473    * Searches for the first occurence of the given argument,
474    * beginning the search at index, and testing for equality
475    * using the equals method.
476    *
477    * @param elem Object to look for
478    * @return the index of the first occurrence of the object
479    * argument in this vector at position index or later in the
480    * vector; returns -1 if the object is not found.
481    */
lastIndexOf(byte elem)482   private  int lastIndexOf(byte elem)
483   {
484     int boffset=m_firstFree%m_blocksize;
485     for(int index=m_firstFree/m_blocksize;
486         index>=0;
487         --index)
488     {
489       byte[] block=m_map[index];
490       if(block!=null)
491         for(int offset=boffset; offset>=0; --offset)
492           if(block[offset]==elem)
493             return offset+index*m_blocksize;
494       boffset=0; // after first
495     }
496     return -1;
497   }
498 
499 }
500