ArrayLinkedVariables
public class ArrayLinkedVariables 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.
Summary
Public methods |
|
|---|---|
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 |
final 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 |
final float |
Return the value of a variable, 0 if not found |
int |
|
int |
getHead() |
final int |
getId(int index)get Id in mCache.mIndexedVariables given the index |
final int |
getNextIndice(int index)Get the next index in mArrayIndices given the current one |
final 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 |
final void |
put(SolverVariable variable, float value)Insert a variable with a given value in the linked list |
final float |
remove(SolverVariable variable, boolean removeFromDefinition)Remove a variable from the list |
int |
Show size in bytes |
String |
toString()Returns a string representation of the list |
float |
Update the current list with a new definition |
Public methods
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
| Parameters | |
|---|---|
SolverVariable variable |
the variable we want to add |
float value |
its value |
contains
public boolean contains(SolverVariable variable)
Returns true if the variable is contained in the list
| Parameters | |
|---|---|
SolverVariable variable |
the variable we are looking for |
| Returns | |
|---|---|
boolean |
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
| Parameters | |
|---|---|
float amount |
amount to divide by |
get
public final float get(SolverVariable v)
Return the value of a variable, 0 if not found
| Parameters | |
|---|---|
SolverVariable v |
the variable we are looking up |
| Returns | |
|---|---|
float |
the value of the found variable, or 0 if not found |
getId
public final int getId(int index)
get Id in mCache.mIndexedVariables given the index
getNextIndice
public final int getNextIndice(int index)
Get the next index in mArrayIndices given the current one
getValue
public final float getValue(int index)
get value in mArrayValues given the index
getVariable
public SolverVariable getVariable(int index)
Return a variable from its position in the linked list
| Parameters | |
|---|---|
int index |
the index of the variable we want to return |
| Returns | |
|---|---|
SolverVariable |
the variable found, or null |
getVariableValue
public float getVariableValue(int index)
Return the value of a variable from its position in the linked list
| Parameters | |
|---|---|
int index |
the index of the variable we want to look up |
| Returns | |
|---|---|
float |
the value of the found variable, or 0 if not found |
put
public final void put(SolverVariable variable, float value)
Insert a variable with a given value in the linked list
| Parameters | |
|---|---|
SolverVariable variable |
the variable to add in the list |
float value |
the value of the variable |
remove
public final float remove(SolverVariable variable, boolean removeFromDefinition)
Remove a variable from the list
| Parameters | |
|---|---|
SolverVariable variable |
the variable we want to remove |
| Returns | |
|---|---|
float |
the value of the removed variable |