Class XStack<V extends XTable<? extends XKey,​? extends XThing>>

  • All Implemented Interfaces:
    Serializable
    Direct Known Subclasses:
    FStack, MIStack, MTStack, VStack

    public abstract class XStack<V extends XTable<? extends XKey,​? extends XThing>>
    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 functions, variables, modules etc., hence the prefix of X for it and things related to it.

    Usage

    Create a subclass as needed for your objects. This involves an XStack, XTable, XKey and an XThing.

    How state is managed

    If we had the following QDL:
         f(x)->x^2;
         block[f(x)->x^3;...]
     
    Then XStack subclass for functions inside the block would look like
         table       entry
         0           f(x)->x^3
         1           f(x)->x^2
     
    Calls to get(XKey) 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 XStack would return f(x)->x^2.

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

    See Also:
    Serialized Form
    • Constructor Detail

      • XStack

        public XStack()
    • Method Detail

      • clear

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

        public void addTables​(XStack xStack)
        Take an XStack and prepend in the correct order to the front of the stack. If xStack 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(XKey) starts from index 0, so local overrides are first!
        Parameters:
        xStack -
      • appendTables

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

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

        public abstract XStack newInstance()
      • newTableInstance

        public abstract XTable 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​(XKey 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​(XKey key)
      • localGet

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

        public XTable<? extends XKey,​? extends XThing> getLocal()
        Get the local table for this stack.
        Returns:
      • localHas

        public boolean localHas​(XKey xkey)
      • nonlocalGet

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

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

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

        public boolean isEmpty()
      • push

        public void push​(XTable<? extends XKey,​? extends XThing> xTable)
      • pushNewTable

        public void pushNewTable()
      • localPut

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

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

        public void remove​(XKey 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)
      • getXMLStackTag

        public abstract String getXMLStackTag()
      • getXMLTableTag

        public abstract String getXMLTableTag()
        Deprecated.
        Returns:
      • keySet

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

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

        public abstract void setStateStack​(State state,
                                           XStack xStack)
        This sets the stack corresponding to this class from the state with the given stack. If the stack is not of the correct type, a class cast exception will result.

        We could have tried this with some type of dynamic casting, but that is messy and fragile in Java
        Parameters:
        state -
        xStack -
      • getStateStack

        public abstract XStack getStateStack​(State state)
        This gets the stack corresponding to this class from the state..
        Parameters:
        state -
        Returns:
      • fromJSON

        public void fromJSON​(net.sf.json.JSONArray array,
                             SerializationState serializationState)
        Processes the array of arrays. Each table is turned into an array of entries. The stack is then an array of these (so we have order of the tables). This will take the entire set of items and recreate the stack structure.
        Parameters:
        array -
        serializationState -
      • serializeContent

        protected void serializeContent​(XMLStreamWriter xsw,
                                        SerializationState serializationState)
                                 throws XMLStreamException
        Very simple (and simple-minded) way to do it. Hand off the serialization so that the tables are converted to JSON arrays with base64 encoded QDL as the entries.. Deserialization then is just interpreting that back. This allows for serializing enormously complex stems and such without having to try and get some XML format for them.
        Parameters:
        xsw -
        serializationState -
        Throws:
        XMLStreamException
      • deserializeFromJSON

        public void deserializeFromJSON​(net.sf.json.JSONObject jsonObject,
                                        SerializationState serializationState,
                                        State state)
      • deserializeFromJSONNEW

        public void deserializeFromJSONNEW​(net.sf.json.JSONObject jsonObject,
                                           SerializationState serializationState,
                                           State state)
      • deserializeFromJSONOLD

        public void deserializeFromJSONOLD​(net.sf.json.JSONObject jsonObject,
                                           SerializationState serializationState,
                                           State state)