Home | History | Annotate | Download | only in compiler
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one
      3  * or more contributor license agreements. See the NOTICE file
      4  * distributed with this work for additional information
      5  * regarding copyright ownership. The ASF licenses this file
      6  * to you under the Apache License, Version 2.0 (the  "License");
      7  * you may not use this file except in compliance with the License.
      8  * You may obtain a copy of the License at
      9  *
     10  *     http://www.apache.org/licenses/LICENSE-2.0
     11  *
     12  * Unless required by applicable law or agreed to in writing, software
     13  * distributed under the License is distributed on an "AS IS" BASIS,
     14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15  * See the License for the specific language governing permissions and
     16  * limitations under the License.
     17  */
     18 /*
     19  * $Id$
     20  */
     21 
     22 package org.apache.xpath.compiler;
     23 
     24 /**
     25  *
     26  * Like IntVector, but used only for the OpMap array.  Length of array
     27  * is kept in the m_lengthPos position of the array.  Only the required methods
     28  * are in included here.
     29  * @xsl.usage internal
     30  */
     31 public class OpMapVector {
     32 
     33  /** Size of blocks to allocate          */
     34   protected int m_blocksize;
     35 
     36   /** Array of ints          */
     37   protected int m_map[]; // IntStack is trying to see this directly
     38 
     39   /** Position where size of array is kept          */
     40   protected int m_lengthPos = 0;
     41 
     42   /** Size of array          */
     43   protected int m_mapSize;
     44 
     45     /**
     46    * Construct a OpMapVector, using the given block size.
     47    *
     48    * @param blocksize Size of block to allocate
     49    */
     50   public OpMapVector(int blocksize, int increaseSize, int lengthPos)
     51   {
     52 
     53     m_blocksize = increaseSize;
     54     m_mapSize = blocksize;
     55     m_lengthPos = lengthPos;
     56     m_map = new int[blocksize];
     57   }
     58 
     59   /**
     60    * Get the nth element.
     61    *
     62    * @param i index of object to get
     63    *
     64    * @return object at given index
     65    */
     66   public final int elementAt(int i)
     67   {
     68     return m_map[i];
     69   }
     70 
     71     /**
     72    * Sets the component at the specified index of this vector to be the
     73    * specified object. The previous component at that position is discarded.
     74    *
     75    * The index must be a value greater than or equal to 0 and less
     76    * than the current size of the vector.
     77    *
     78    * @param value object to set
     79    * @param index Index of where to set the object
     80    */
     81   public final void setElementAt(int value, int index)
     82   {
     83     if (index >= m_mapSize)
     84     {
     85       int oldSize = m_mapSize;
     86 
     87       m_mapSize += m_blocksize;
     88 
     89       int newMap[] = new int[m_mapSize];
     90 
     91       System.arraycopy(m_map, 0, newMap, 0, oldSize);
     92 
     93       m_map = newMap;
     94     }
     95 
     96     m_map[index] = value;
     97   }
     98 
     99 
    100   /*
    101    * Reset the array to the supplied size.  No checking is done.
    102    *
    103    * @param size The size to trim to.
    104    */
    105   public final void setToSize(int size) {
    106 
    107     int newMap[] = new int[size];
    108 
    109     System.arraycopy(m_map, 0, newMap, 0, m_map[m_lengthPos]);
    110 
    111     m_mapSize = size;
    112     m_map = newMap;
    113 
    114   }
    115 
    116 }
    117