001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: OpMapVector.java 1225426 2011-12-29 04:13:08Z mrglavas $
020     */
021    
022    package org.apache.xpath.compiler;
023    
024    /**
025     * 
026     * Like IntVector, but used only for the OpMap array.  Length of array
027     * is kept in the m_lengthPos position of the array.  Only the required methods 
028     * are in included here.
029     * @xsl.usage internal
030     */
031    public class OpMapVector {
032        
033     /** Size of blocks to allocate          */
034      protected int m_blocksize;
035    
036      /** Array of ints          */
037      protected int m_map[]; // IntStack is trying to see this directly
038    
039      /** Position where size of array is kept          */
040      protected int m_lengthPos = 0;
041    
042      /** Size of array          */
043      protected int m_mapSize;
044      
045        /**
046       * Construct a OpMapVector, using the given block size.
047       *
048       * @param blocksize Size of block to allocate
049       */
050      public OpMapVector(int blocksize, int increaseSize, int lengthPos)
051      {
052    
053        m_blocksize = increaseSize;
054        m_mapSize = blocksize;
055        m_lengthPos = lengthPos;
056        m_map = new int[blocksize];
057      }
058      
059      /**
060       * Get the nth element.
061       *
062       * @param i index of object to get
063       *
064       * @return object at given index
065       */
066      public final int elementAt(int i)
067      {
068        return m_map[i];
069      }  
070       
071        /**
072       * Sets the component at the specified index of this vector to be the
073       * specified object. The previous component at that position is discarded.
074       *
075       * The index must be a value greater than or equal to 0 and less
076       * than the current size of the vector.
077       *
078       * @param value object to set
079       * @param index Index of where to set the object
080       */
081      public final void setElementAt(int value, int index)
082      {
083        if (index >= m_mapSize)
084        {
085          int oldSize = m_mapSize;
086          
087          m_mapSize += m_blocksize;
088    
089          int newMap[] = new int[m_mapSize];
090    
091          System.arraycopy(m_map, 0, newMap, 0, oldSize);
092    
093          m_map = newMap;
094        }
095    
096        m_map[index] = value;
097      }
098      
099      
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    }