FileDocCategorySizeDatePackage
stack.javaAPI DocExample4921Sat Nov 25 11:54:30 GMT 2000None

TokenStack

public class TokenStack extends Object
A stack which can hold only m_iMaxSize elements Purpose: Stack used by Parser

Fields Summary
private int
m_iInsertLoc
private int
m_iSize
private int
m_iMask
static final int
m_iMaxSize
private static boolean
m_bDebug
Vector
m_Vector
Constructors Summary
public TokenStack()
Purpose: Constructor

 
		int iTest = m_iMaxSize;
	
	while((iTest & 1) != 1)	
		iTest = iTest >> 1;
		
	
		if((iTest>>1)!=0){ 
			System.out.println("m_iMaxSize should be a power of two");
		
			System.exit(1);
		}
	
		
		m_Vector = new Vector(m_iMaxSize);
		
	
		m_iSize = 0;
	    m_iInsertLoc = 0;
	    m_iMask = m_iMaxSize -1;
		initialize();
	
	
Methods Summary
public java.lang.StringElementFromTopOfTheStack(int iIndex)
Purpose: gets an element from the top of the stack using zero-based iIndex

param
iIndex
return
String

	
		
		if(iIndex < 0 || iIndex > (GetSize()-1) )
			return "";
		iIndex = (GetStartPos() - iIndex) & m_iMask;
		return (String)m_Vector.elementAt(iIndex);
	
private intGetNext(int iCurrent)
Purpose:

return
int
param
iCurrent

 return (iCurrent & m_iMask); 
public intGetSize()
Purpose: Returns the constant size of the stack

return
int

 return  m_iMaxSize; 
private intGetStartPos()
Purpose:

return
int

      
	
	   return (m_iInsertLoc -1) & m_iMask; 
public java.lang.StringTokenAt(int iIndex)
Purpose: Gets a token from the top of the stack Note: This function was written to maintain the compatability with ParseStack class that uses GetSize() to call TokenAt() Later, Parser should be modified to call ElementFromTopOfTheStack()

 
	    
		if(iIndex < 0 || iIndex > m_iMaxSize-1) 
			return "";
		
		iIndex = m_iMaxSize-iIndex-1;
		return 	ElementFromTopOfTheStack(iIndex);
	
public java.lang.StringgetClassNameFromStack(int iDepth)
Purpose: searches stack up to iDepth for a class name

param
iDepth
return
String

		String strClassName = new String();
		int iSize = GetSize(), n;
		for(n = iDepth; n < iSize - iDepth -2; n+=3){
			if( TokenAt(iSize-(n+1)).compareTo(":") != 0  ||
			    TokenAt(iSize-(n+2)).compareTo(":")!= 0 )
				break;
		}
	
		if(m_bDebug)printStack(n);
	
		for(; n > iDepth; n-=3)
			strClassName += TokenAt(iSize-n);
		strClassName+=TokenAt(iSize-iDepth);
		
		return strClassName;

	
private voidinitialize()
Purpose:

		for(int i = 0; i < m_iMaxSize; i++)
			m_Vector.addElement("");
	
	
public static voidmain(java.lang.String[] args)
Purpose:

	 TokenStack stack = new TokenStack();
	 stack.push("A");
	 stack.push("B");
	 stack.push("C");
	 stack.push("D");
	 stack.push("E");
	 stack.push("F");
	 stack.push("G");
	 stack.push("H");
	 stack.push("I");
	 stack.push("J");
	 stack.push("K");
	 stack.push("L");

	 stack.printStack(5);
 	 System.out.println(stack.TokenAt(stack.GetSize() - 1));
	 System.out.println(stack.TokenAt(stack.GetSize() -2));
	 System.out.println(stack.TokenAt(stack.GetSize() -3));
 	 System.out.println(stack.ElementFromTopOfTheStack(0));
	 System.out.println(stack.ElementFromTopOfTheStack(1));
	 System.out.println(stack.ElementFromTopOfTheStack(2));

 
 
 
 
public voidprintStack(int iDepth)
Purpose: prints the contents of the stack up the iDepth

param
iDepth

		
		int iSize = GetSize();
		if( iDepth > iSize -1 )	System.out.println("Unable to print stack to " + String.valueOf(iDepth) + " depth");

		else{
			
			System.out.println("Printing the TokenStack: ");
			
			for(int n = 0; n < iDepth; n++){
				System.out.print(n + "th element from the top of the stack -  ");
				System.out.println(ElementFromTopOfTheStack(n));
			}
		}

	
	
public voidpush(java.lang.String obj)
Purpose: pushes the stack Note: if there m_iMaxSize elements on the stack, the bottom most element is removed

		m_Vector.setElementAt(obj, m_iInsertLoc);
		m_iSize ++;
		m_iInsertLoc = m_iSize % m_iMaxSize;
		if(m_iSize == m_iMaxSize)
		   m_iSize = 0;