Class ArrayLinkedVariables

  • All Implemented Interfaces:
    ArrayRow.ArrayRowVariables

    public class ArrayLinkedVariables
    extends java.lang.Object
    implements ArrayRow.ArrayRowVariables
    Store a set of variables and their values in an array-based linked list. The general idea is that we want to store a list of variables that need to be ordered, space efficient, and relatively fast to maintain (add/remove). ArrayBackedVariables implements a sparse array, so is rather space efficient, but maintaining the array sorted is costly, as we spend quite a bit of time recopying parts of the array on element deletion. LinkedVariables implements a standard linked list structure, and is able to be faster than ArrayBackedVariables even though it's more costly to set up (pool of objects...), as the elements removal and maintenance of the structure is a lot more efficient. This ArrayLinkedVariables class takes inspiration from both of the above, and implement a linked list stored in several arrays. This allows us to be a lot more efficient in terms of setup (no need to deal with pool of objects...), resetting the structure, and insertion/deletion of elements.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected Cache mCache  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(SolverVariable variable, float value, boolean removeFromDefinition)
      Add value to an existing variable The code is broadly identical to the put() method, only differing in in-line deletion, and of course doing an add rather than a put
      void clear()
      Clear the list of variables
      boolean contains​(SolverVariable variable)
      Returns true if the variable is contained in the list
      void display()
      print out the variables and their values
      void divideByAmount​(float amount)
      Divide the values of all the variables in the list by the given amount
      float get​(SolverVariable v)
      Return the value of a variable, 0 if not found
      int getCurrentSize()  
      int getHead()  
      int getId​(int index)
      get Id in mCache.mIndexedVariables given the index
      int getNextIndice​(int index)
      Get the next index in mArrayIndices given the current one
      float getValue​(int index)
      get value in mArrayValues given the index
      SolverVariable getVariable​(int index)
      Return a variable from its position in the linked list
      float getVariableValue​(int index)
      Return the value of a variable from its position in the linked list
      int indexOf​(SolverVariable variable)  
      void invert()
      Invert the values of all the variables in the list
      void put​(SolverVariable variable, float value)
      Insert a variable with a given value in the linked list
      float remove​(SolverVariable variable, boolean removeFromDefinition)
      Remove a variable from the list
      int sizeInBytes()
      Show size in bytes
      java.lang.String toString()
      Returns a string representation of the list
      float use​(ArrayRow definition, boolean removeFromDefinition)
      Update the current list with a new definition
      • Methods inherited from class java.lang.Object

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

      • mCache

        protected final Cache mCache
    • Method Detail

      • put

        public final void put​(SolverVariable variable,
                              float value)
        Insert a variable with a given value in the linked list
        Specified by:
        put in interface ArrayRow.ArrayRowVariables
        Parameters:
        variable - the variable to add in the list
        value - the value of the variable
      • add

        public void add​(SolverVariable variable,
                        float value,
                        boolean removeFromDefinition)
        Add value to an existing variable The code is broadly identical to the put() method, only differing in in-line deletion, and of course doing an add rather than a put
        Specified by:
        add in interface ArrayRow.ArrayRowVariables
        Parameters:
        variable - the variable we want to add
        value - its value
      • use

        public float use​(ArrayRow definition,
                         boolean removeFromDefinition)
        Update the current list with a new definition
        Specified by:
        use in interface ArrayRow.ArrayRowVariables
        Parameters:
        definition - the row containing the definition
      • remove

        public final float remove​(SolverVariable variable,
                                  boolean removeFromDefinition)
        Remove a variable from the list
        Specified by:
        remove in interface ArrayRow.ArrayRowVariables
        Parameters:
        variable - the variable we want to remove
        Returns:
        the value of the removed variable
      • contains

        public boolean contains​(SolverVariable variable)
        Returns true if the variable is contained in the list
        Specified by:
        contains in interface ArrayRow.ArrayRowVariables
        Parameters:
        variable - the variable we are looking for
        Returns:
        return true if we found the variable
      • divideByAmount

        public void divideByAmount​(float amount)
        Divide the values of all the variables in the list by the given amount
        Specified by:
        divideByAmount in interface ArrayRow.ArrayRowVariables
        Parameters:
        amount - amount to divide by
      • getHead

        public int getHead()
      • getId

        public final int getId​(int index)
        get Id in mCache.mIndexedVariables given the index
      • getValue

        public final float getValue​(int index)
        get value in mArrayValues given the index
      • getNextIndice

        public final int getNextIndice​(int index)
        Get the next index in mArrayIndices given the current one
      • getVariable

        public SolverVariable getVariable​(int index)
        Return a variable from its position in the linked list
        Specified by:
        getVariable in interface ArrayRow.ArrayRowVariables
        Parameters:
        index - the index of the variable we want to return
        Returns:
        the variable found, or null
      • getVariableValue

        public float getVariableValue​(int index)
        Return the value of a variable from its position in the linked list
        Specified by:
        getVariableValue in interface ArrayRow.ArrayRowVariables
        Parameters:
        index - the index of the variable we want to look up
        Returns:
        the value of the found variable, or 0 if not found
      • get

        public final float get​(SolverVariable v)
        Return the value of a variable, 0 if not found
        Specified by:
        get in interface ArrayRow.ArrayRowVariables
        Parameters:
        v - the variable we are looking up
        Returns:
        the value of the found variable, or 0 if not found
      • toString

        public java.lang.String toString()
        Returns a string representation of the list
        Overrides:
        toString in class java.lang.Object
        Returns:
        a string containing a representation of the list