Class SymbolTable


  • public class SymbolTable
    extends 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 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​(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​(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​(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​(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 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 String toString()
        Returns the string representation of the internal hash table.
        Overrides:
        toString in class Object
        Returns:
        A string representing the symbol table.
      • readFromFile

        public static SymbolTable readFromFile​(String filename)
                                        throws 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:
        IOException - Errors reading in the file.