Class SStack<V extends STable<? extends SKey,​? extends SThing>>

  • All Implemented Interfaces:
    Serializable
    Direct Known Subclasses:
    EnvStack

    public abstract class SStack<V extends STable<? extends SKey,​? extends SThing>>
    extends Object
    implements Serializable
    A stateful stack of things, such as functions. This is the method by which local state is preserved. The zero-th element is the current local table. It is used for entries., hence the prefix of S for it and things related to it.

    Usage

    Create a subclass as needed for your objects. This involves an SStack, STable, SKey and an SThing.

    How state is managed

    If we had the following QDL:
         f(x)->x^2;
         block[f(x)->x^3;...]
     
    Then SStack subclass for functions inside the block would look like
         table       entry
         0           f(x)->x^3
         1           f(x)->x^2
     
    Calls to get(SKey) would peek at 0 and return f(x)->x^3 inside the block. This is how local state overrides the parent state. Blocks of course can be for loops, functions, conditionals etc. Were there no entry for f(x) in the block, then SStack would return f(x)->x^2.

    Created by Jeff Gaynor
    on 11/8/21 at 6:27 AM

    See Also:
    Serialized Form
    • Constructor Detail

      • SStack

        public SStack()
    • Method Detail

      • clear

        public void clear()
        Clears the entire stack and resets it.
      • addTables

        public void addTables​(SStack sStack)
        Take an SStack and prepend in the correct order to the front of the stack. If SStack is [A,B,C,...] And the existing stack is [P,Q,...] the result is [A,B,C,...,P,Q,...] This is needed when, e.g., creating new local state for function reference resolution

        Note: get(SKey) starts from index 0, so local overrides are first!
        Parameters:
        sStack -
      • appendTables

        public void appendTables​(SStack sStack)
        Similar to addTables(SStack), but this appends them to the existing set of tables. If SStack is [A,B,C,...] And the existing stack is [P,Q,...] the result is [P,Q,...,A,B,C,...,]

        Note: get(SKey) starts from index 0, so local overrides are first!
        Parameters:
        sStack -
      • newInstance

        public abstract SStack newInstance()
      • newTableInstance

        public abstract STable newTableInstance()
      • append

        public void append​(V v)
        Append the table to the end of the stack -- this sets the root for the table.
        Parameters:
        v -
      • containsKey

        public boolean containsKey​(SKey key,
                                   int startTableIndex)
        Check that a specific key is in a table starting at the index. This lets you, e.g., skip local state if the start index is positive.
        Parameters:
        key -
        startTableIndex -
        Returns:
      • containsKey

        public boolean containsKey​(SKey key)
      • localGet

        public SThing localGet​(SKey key)
        Only returns a non-null element if it is defined in the local (index 0) table.
        Parameters:
        key -
        Returns:
      • getLocal

        public STable<? extends SKey,​? extends SThing> getLocal()
        Get the local table for this stack.
        Returns:
      • localHas

        public boolean localHas​(SKey xkey)
      • get

        public SThing get​(SKey key)
        Get the value from someplace in the stack. This returns first found so manages the overrides in the scope. Note that local calls (e.g. localGet(SKey) only look in the current scope.
        Parameters:
        key -
        Returns:
      • nonlocalGet

        public SThing nonlocalGet​(SKey key)
        searches for the entry every place except the most local state.
        Parameters:
        key -
        Returns:
      • getAll

        public List<? extends SThing> getAll()
        Get all of the values from all tables.This returns a flat list.
        Returns:
      • getRoot

        public STable<? extends SKey,​? extends SThing> getRoot()
        Since all new tables are added at 0, the initial one, called the root, is last. This gets the root STable.
        Returns:
      • isEmpty

        public boolean isEmpty()
      • push

        public void push​(STable<? extends SKey,​? extends SThing> sTable)
      • pushNewTable

        public void pushNewTable()
      • localPut

        public SThing localPut​(SThing value)
        Only add this to the local state.
        Parameters:
        value -
        Returns:
      • localRemove

        public void localRemove​(SKey key)
        Removes only the most local entry.
        Parameters:
        key -
      • remove

        public void remove​(SKey key)
        Removes all references from all tables. This includes all overrides so at the end of this operation there are no references any place.
        Parameters:
        key -
      • size

        public int size()
      • toString

        public String toString​(boolean showKeys)
      • keySet

        public Set<SKey> keySet()
        Returns the unique set of keys over the tables.
        Returns:
      • allKeys

        public List<SKey> allKeys()
        Returns the a list of keys (including redundancies) for this stack.
        Returns:
      • getAsMap

        public Map<String,​String> getAsMap()
        Get a read only virtualization of this stack as a map.
        Returns: