Class SymbolTable


  • public class SymbolTable
    extends java.lang.Object
    The SymbolTable class provides a generic symbol table. A symbol table is a bijective mapping between strings and integers. Symbol numbering starts at a point defined by the user, which is 0 by default. A SymbolTable provides quick access to symbol numbers (code points) through a hash table. The reverse map is facilitated through a separate vector of the symbols. This is memory intensive, but much faster than searching through the hashtable.
    • Constructor Summary

      Constructors 
      Constructor Description
      SymbolTable()
      Default constructor
      SymbolTable​(int start)
      Use this constructor if you need your symbol numbering to start at a different point than 0.
      SymbolTable​(java.lang.String[] names)
      Init the symbol table from an array of strings, where code points correspond to array positions.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean contains​(java.lang.String symbol)  
      SymbolTable copy()
      Perform a deep copy.
      int get​(java.lang.String symbol)
      Get value of symbol in table.
      int getStart()
      Get the starting number of the symbol table.
      java.lang.String getSymbol​(int i)
      Find the symbol corresponding to a value.
      static SymbolTable readFromFile​(java.lang.String filename)
      Read a file line by line, and create an entry for each line.
      int set​(java.lang.String symbol)
      Create new symbol in table.
      int size()
      Returns number of symbols in table.
      java.lang.String toString()
      Returns the string representation of the internal hash table.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • SymbolTable

        public SymbolTable​(int start)
        Use this constructor if you need your symbol numbering to start at a different point than 0.
        Parameters:
        start - The code point of the first symbol added to the table. Subsequent symbols will have larger code points.
      • SymbolTable

        public SymbolTable()
        Default constructor
      • SymbolTable

        public SymbolTable​(java.lang.String[] names)
        Init the symbol table from an array of strings, where code points correspond to array positions. Warning: if the array contains duplicate strings, the numbering will be off!
        Parameters:
        names - The String array containing the symbols.
    • Method Detail

      • contains

        public boolean contains​(java.lang.String symbol)
      • getStart

        public int getStart()
        Get the starting number of the symbol table.
        Returns:
        The start of the table.
      • copy

        public SymbolTable copy()
        Perform a deep copy.
        Returns:
        A copy of this.
      • set

        public int set​(java.lang.String symbol)
        Create new symbol in table. If the symbol already exists, only the symbol's number is returned. Use get() if you need to find out if the symbol is already in the table.
        Parameters:
        symbol - the input string to be put in the table.
        Returns:
        the symbol's number.
      • get

        public int get​(java.lang.String symbol)
        Get value of symbol in table.
        Parameters:
        symbol - the input string.
        Returns:
        the symbol's number, if the symbol is in the table, and start-1, else (where start is the code point of the first symbol).
      • getSymbol

        public java.lang.String getSymbol​(int i)
        Find the symbol corresponding to a value.
        Parameters:
        i - the number that we want to get the string value for.
        Returns:
        the symbol corresponding to the input number, if it exists, and null, else.
      • size

        public int size()
        Returns number of symbols in table.
        Returns:
        The number of symbols in the table.
      • toString

        public java.lang.String toString()
        Returns the string representation of the internal hash table.
        Overrides:
        toString in class java.lang.Object
        Returns:
        A string representing the symbol table.
      • readFromFile

        public static SymbolTable readFromFile​(java.lang.String filename)
                                        throws java.io.IOException
        Read a file line by line, and create an entry for each line. Empty lines are ignored. Lines containing only whitespace are not.
        Parameters:
        filename - The name of the file to be read in.
        Returns:
        The SymbolTable created from the file.
        Throws:
        java.io.IOException - Errors reading in the file.