fixed unnecessary calculations

This commit is contained in:
Denis Molony 2017-03-20 18:17:46 +11:00
parent 7eb21ec5b7
commit e3b6e56c20
8 changed files with 34 additions and 103 deletions

View File

@ -10,19 +10,12 @@ public abstract class AbstractValue implements Value, Iterable<Value>
protected double value;
protected ValueType valueType = ValueType.VALUE;
protected List<Value> values = new ArrayList<Value> ();
protected boolean isVolatile = true;
public AbstractValue (String typeText)
{
this.typeText = typeText;
}
@Override
public boolean isVolatile ()
{
return isVolatile;
}
@Override
public String getTypeText ()
{

View File

@ -16,6 +16,7 @@ class Cell extends AbstractValue implements Comparable<Cell>
private String repeat = "";
private String label;
private Value value;
private boolean hasCalculated;
enum CellType
{
@ -30,7 +31,11 @@ class Cell extends AbstractValue implements Comparable<Cell>
this.address = address;
cellType = CellType.EMPTY;
isVolatile = false;
}
void reset ()
{
hasCalculated = false;
}
Cell getCell (Address address)
@ -68,12 +73,6 @@ class Cell extends AbstractValue implements Comparable<Cell>
return parent;
}
@Override
public boolean isVolatile ()
{
return isVolatile;
}
Address getAddress ()
{
return address;
@ -109,7 +108,6 @@ class Cell extends AbstractValue implements Comparable<Cell>
for (int i = 0; i < 20; i++)
repeat += repeatingText;
cellType = CellType.REPEATING_CHARACTER;
isVolatile = false;
return;
}
@ -124,7 +122,6 @@ class Cell extends AbstractValue implements Comparable<Cell>
{
label = command.substring (1);
cellType = CellType.LABEL;
isVolatile = false;
}
else
{
@ -133,7 +130,6 @@ class Cell extends AbstractValue implements Comparable<Cell>
expressionText = command;
value = new Expression (this, expressionText).reduce ();
cellType = CellType.VALUE;
isVolatile = true;
}
catch (IllegalArgumentException e)
{
@ -268,16 +264,17 @@ class Cell extends AbstractValue implements Comparable<Cell>
{
if (cellType == CellType.VALUE)
{
if (isVolatile)
if (!hasCalculated)
{
value.calculate ();
isVolatile = value.isVolatile ();
hasCalculated = true;
}
}
}
public String getDebugText ()
{
boolean isVolatile = false;
StringBuilder text = new StringBuilder ();
text.append (line);
text.append ("\n");
@ -336,8 +333,7 @@ class Cell extends AbstractValue implements Comparable<Cell>
contents = "Empty";
}
return String.format ("[Cell:%5s %s %s]", address, contents,
isVolatile ? "volatile" : "fixed");
return String.format ("[Cell:%5s %s]", address, contents);
}
@Override

View File

@ -143,30 +143,10 @@ class Expression extends AbstractValue //implements Iterable<Value>
return;
}
if (!isVolatile)
return;
boolean currentVolatile = false;
// boolean debug = cell.getAddress ().matches ("C8");
boolean debug = false;
if (debug)
{
System.out.println (this);
System.out.printf ("(1) exp %s is currently %svolatile%n", text,
isVolatile ? " " : "not ");
}
try
{
Value thisValue = values.get (0);
thisValue.calculate ();
if (debug)
{
System.out.println (this);
System.out.printf ("(2) exp %s is currently %svolatile%n", thisValue.getText (),
thisValue.isVolatile () ? " " : "not ");
}
value = 0;
if (!thisValue.isValueType (ValueType.VALUE))
@ -176,8 +156,6 @@ class Expression extends AbstractValue //implements Iterable<Value>
}
value = thisValue.getValue ();
if (!currentVolatile)
currentVolatile = thisValue.isVolatile ();
String sign = signs.get (0);
if (sign.equals ("(-)"))
@ -195,14 +173,6 @@ class Expression extends AbstractValue //implements Iterable<Value>
}
double nextValue = thisValue.getValue ();
if (debug)
{
System.out.println (this);
System.out.printf ("(3.%d) exp %s is currently %svolatile%n", i,
thisValue.getText (), thisValue.isVolatile () ? " " : "not ");
}
if (!currentVolatile)
currentVolatile = thisValue.isVolatile ();
sign = signs.get (i);
if (sign.equals ("(-)"))
@ -239,15 +209,6 @@ class Expression extends AbstractValue //implements Iterable<Value>
e.printStackTrace ();
return;
}
isVolatile = currentVolatile;
if (debug)
{
System.out.println (this);
System.out.printf ("(4) exp %s is currently %svolatile%n", text,
isVolatile ? " " : "not ");
System.out.println ();
}
}
private String balanceBrackets (String input)
@ -385,12 +346,6 @@ class Expression extends AbstractValue //implements Iterable<Value>
return text.toString ();
}
// @Override
// public Iterator<Value> iterator ()
// {
// return values.iterator ();
// }
@Override
public String toString ()
{

View File

@ -11,8 +11,7 @@ class Lookup extends ValueListFunction
@Override
public void calculate ()
{
Value source = list.get (0);
Value source = list.get (0); // first Value is the value to look up
source.calculate ();
if (!source.isValueType (ValueType.VALUE))
@ -29,6 +28,8 @@ class Lookup extends ValueListFunction
double sourceValue = source.getValue ();
Address target = null;
// is the range horizontal or vertical?
Cell firstCell = (Cell) list.get (1);
Cell lastCell = (Cell) list.get (list.size () - 1);
boolean isVertical = firstCell.getAddress ().columnMatches (lastCell.getAddress ());
@ -38,28 +39,26 @@ class Lookup extends ValueListFunction
Cell cell = (Cell) list.get (i);
if (cell.getValue () > sourceValue) // past the value
break;
target = cell.getAddress ();
target = cell.getAddress (); // this could be the one
}
if (target == null)
{
valueType = ValueType.NA;
value = 0;
return;
}
Address adjacentAddress = isVertical ? target.nextColumn () : target.nextRow ();
if (cell.cellExists (adjacentAddress))
{
value = cell.getCell (adjacentAddress).getValue ();
valueType = ValueType.VALUE;
}
else
{
Address adjacentAddress = isVertical ? target.nextColumn () : target.nextRow ();
if (cell.cellExists (adjacentAddress))
{
value = cell.getCell (adjacentAddress).getValue ();
valueType = ValueType.VALUE;
}
else
{
value = 0;
valueType = ValueType.VALUE;
}
value = 0;
valueType = ValueType.VALUE;
}
}
}

View File

@ -16,7 +16,7 @@ public class Npv extends ValueListFunction
value = 0;
valueType = ValueType.VALUE;
Value source = list.get (0);
Value source = list.get (0); // first Value is the rate
source.calculate ();
if (!source.isValueType (ValueType.VALUE))
{
@ -25,15 +25,13 @@ public class Npv extends ValueListFunction
}
double rate = 1 + source.getValue ();
int period = 0;
int pos = 0;
for (int i = 1; i < list.size (); i++)
{
Cell cell = (Cell) list.get (i);
for (int i = 1; i < list.size (); i++) // remaining Values are Cells
{
++period;
Cell cell = (Cell) list.get (i);
if (cell.isCellType (CellType.EMPTY))
continue;

View File

@ -9,7 +9,6 @@ class Number extends AbstractValue
try
{
value = Double.parseDouble (text);
isVolatile = false;
}
catch (NumberFormatException e)
{

View File

@ -171,23 +171,16 @@ public class Sheet
}
// might have to keep recalculating until nothing changes??
// System.out.println ("\n*********** Calculating\n");
calculate (recalculationOrder);
// System.out.println ("\n*********** Calculating\n");
// calculate (recalculationOrder);
}
private void calculate (char order)
{
int count = 0;
Map<Integer, Cell> cells = order == 'R' ? rowOrderCells : columnOrderCells;
for (Cell cell : cells.values ())
if (cell.isCellType (CellType.VALUE))
{
// System.out.printf ("%5d start %s%n", count, cell.getAddressText ());
cell.calculate ();
// System.out.printf ("%5d stop %s%n", count++, cell.getAddressText ());
}
}
private int getLineLength (byte[] buffer, int offset)

View File

@ -7,19 +7,17 @@ interface Value
VALUE, ERROR, NA
}
public double getValue (); // if ValueType == VALUE
public String getText (); // if ValueType != VALUE
public boolean isValueType (ValueType valueType);
public ValueType getValueType ();
public double getValue (); // if ValueType == VALUE
public String getText (); // if ValueType != VALUE
public void calculate ();
public String getTypeText (); // Number/Function/Expression etc
public boolean isVolatile ();
public boolean isBoolean ();
public boolean isBoolean (); // display TRUE/FALSE instead of 1/0
}