Home | History | Annotate | Download | only in utils
      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: IntStack.java 468655 2006-10-28 07:12:06Z minchau $
     20  */
     21 package org.apache.xml.utils;
     22 
     23 import java.util.EmptyStackException;
     24 
     25 /**
     26  * Implement a stack of simple integers.
     27  *
     28  * %OPT%
     29  * This is currently based on IntVector, which permits fast acess but pays a
     30  * heavy recopying penalty if/when its size is increased. If we expect deep
     31  * stacks, we should consider a version based on ChunkedIntVector.
     32  * @xsl.usage internal
     33  */
     34 public class IntStack extends IntVector
     35 {
     36 
     37   /**
     38    * Default constructor.  Note that the default
     39    * block size is very small, for small lists.
     40    */
     41   public IntStack()
     42   {
     43     super();
     44   }
     45 
     46   /**
     47    * Construct a IntVector, using the given block size.
     48    *
     49    * @param blocksize Size of block to allocate
     50    */
     51   public IntStack(int blocksize)
     52   {
     53     super(blocksize);
     54   }
     55 
     56   /**
     57    * Copy constructor for IntStack
     58    *
     59    * @param v IntStack to copy
     60    */
     61   public IntStack (IntStack v)
     62   {
     63   	super(v);
     64   }
     65 
     66   /**
     67    * Pushes an item onto the top of this stack.
     68    *
     69    * @param   i   the int to be pushed onto this stack.
     70    * @return  the <code>item</code> argument.
     71    */
     72   public int push(int i)
     73   {
     74 
     75     if ((m_firstFree + 1) >= m_mapSize)
     76     {
     77       m_mapSize += m_blocksize;
     78 
     79       int newMap[] = new int[m_mapSize];
     80 
     81       System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
     82 
     83       m_map = newMap;
     84     }
     85 
     86     m_map[m_firstFree] = i;
     87 
     88     m_firstFree++;
     89 
     90     return i;
     91   }
     92 
     93   /**
     94    * Removes the object at the top of this stack and returns that
     95    * object as the value of this function.
     96    *
     97    * @return     The object at the top of this stack.
     98    */
     99   public final int pop()
    100   {
    101     return m_map[--m_firstFree];
    102   }
    103 
    104   /**
    105    * Quickly pops a number of items from the stack.
    106    */
    107 
    108   public final void quickPop(int n)
    109   {
    110     m_firstFree -= n;
    111   }
    112 
    113   /**
    114    * Looks at the object at the top of this stack without removing it
    115    * from the stack.
    116    *
    117    * @return     the object at the top of this stack.
    118    * @throws  EmptyStackException  if this stack is empty.
    119    */
    120   public final int peek()
    121   {
    122     try {
    123       return m_map[m_firstFree - 1];
    124     }
    125     catch (ArrayIndexOutOfBoundsException e)
    126     {
    127       throw new EmptyStackException();
    128     }
    129   }
    130 
    131   /**
    132    * Looks at the object at the position the stack counting down n items.
    133    *
    134    * @param n The number of items down, indexed from zero.
    135    * @return     the object at n items down.
    136    * @throws  EmptyStackException  if this stack is empty.
    137    */
    138   public int peek(int n)
    139   {
    140     try {
    141       return m_map[m_firstFree-(1+n)];
    142     }
    143     catch (ArrayIndexOutOfBoundsException e)
    144     {
    145       throw new EmptyStackException();
    146     }
    147   }
    148 
    149   /**
    150    * Sets an object at a the top of the statck
    151    *
    152    *
    153    * @param val object to set at the top
    154    * @throws  EmptyStackException  if this stack is empty.
    155    */
    156   public void setTop(int val)
    157   {
    158     try {
    159       m_map[m_firstFree - 1] = val;
    160     }
    161     catch (ArrayIndexOutOfBoundsException e)
    162     {
    163       throw new EmptyStackException();
    164     }
    165   }
    166 
    167   /**
    168    * Tests if this stack is empty.
    169    *
    170    * @return  <code>true</code> if this stack is empty;
    171    *          <code>false</code> otherwise.
    172    * @since   JDK1.0
    173    */
    174   public boolean empty()
    175   {
    176     return m_firstFree == 0;
    177   }
    178 
    179   /**
    180    * Returns where an object is on this stack.
    181    *
    182    * @param   o   the desired object.
    183    * @return  the distance from the top of the stack where the object is]
    184    *          located; the return value <code>-1</code> indicates that the
    185    *          object is not on the stack.
    186    * @since   JDK1.0
    187    */
    188   public int search(int o)
    189   {
    190 
    191     int i = lastIndexOf(o);
    192 
    193     if (i >= 0)
    194     {
    195       return size() - i;
    196     }
    197 
    198     return -1;
    199   }
    200 
    201   /**
    202    * Returns clone of current IntStack
    203    *
    204    * @return clone of current IntStack
    205    */
    206   public Object clone()
    207     throws CloneNotSupportedException
    208   {
    209   	return (IntStack) super.clone();
    210   }
    211 }
    212