package me.cvhc.equationsolver;

import com.androidplot.xy.XYSeries;

public class FunctionWrapper implements XYSeries {
    public interface MathFunction {
        double call(double x);
    }

    MathFunction function;
    int seriesSize;
    double upperBound = 0, lowerBound = 0;
    double step = 0;

    public FunctionWrapper(MathFunction f, int size) {
        function = f;
        seriesSize = size;
    }

    public void setBound(Double l, Double u) {
        if (l != null) { lowerBound = l; }
        if (u != null) { upperBound = u; }
        step = (upperBound - lowerBound) / seriesSize;
    }

    public double getLowerBound() {
        return lowerBound;
    }

    public double getUpperBound() {
        return upperBound;
    }

    @Override
    public int size() {
        return seriesSize;
    }

    @Override
    public Number getX(int index) {
        return lowerBound + step * index;
    }

    @Override
    public Number getY(int index) {
        return function.call(getX(index).doubleValue());
    }

    @Override
    public String getTitle() {
        return "What title?";
    }
}

@@ -37,35 +37,123 @@
 ort 
-com.androidplot.xy.XYSeries
+android.util.Pair;%0A%0Aimport com.androidplot.xy.XYSeries;%0A%0Aimport java.util.LinkedList;%0Aimport java.util.ListIterator
 ;%0A%0Ap
@@ -397,300 +397,1501 @@
    p
-ublic FunctionWrapper(MathFunction f, int size) %7B%0A        function = f;%0A        seriesSize = size;%0A    %7D%0A%0A    public void setBound(Double l, Double u) %7B%0A        if (l != null) %7B lowerBound = l; %7D%0A        if (u != null) %7B upperBound = u; %7D%0A        step = (upperBound - lowerBound) / seriesSize
+rivate Pair%3CDouble,Double%3E%5B%5D series;%0A    private LinkedList%3CPair%3CDouble,Double%3E%3E seriesCache = new LinkedList%3C%3E();%0A%0A    public FunctionWrapper(MathFunction f, int size) %7B%0A        function = f;%0A        seriesSize = size;%0A        seriesCache.addFirst(new Pair%3CDouble,Double%3E(Double.NEGATIVE_INFINITY, null));%0A        seriesCache.addLast(new Pair%3CDouble, Double%3E(Double.POSITIVE_INFINITY, null));%0A    %7D%0A%0A    public void setBound(Double l, Double u) %7B%0A        if (l != null) %7B lowerBound = l; %7D%0A        if (u != null) %7B upperBound = u; %7D%0A        step = (upperBound - lowerBound) / seriesSize;%0A%0A        ListIterator%3CPair%3CDouble,Double%3E%3E iter = seriesCache.listIterator();%0A        Double x = lowerBound;%0A%0A        series = new Pair%5BseriesSize%5D;%0A        for (int i=0; i%3CseriesSize; i++, x+=step) %7B%0A            double lower = x - step/2.0;%0A            double upper = x + step/2.0;%0A            while (iter.hasNext()) %7B%0A                Pair%3CDouble,Double%3E cur = iter.next();%0A                if (cur.first %3C= lower) %7B%0A                    iter.remove();%0A                %7D else if (cur.first %3C upper) %7B%0A                    series%5Bi%5D = cur;%0A                    break;%0A                %7D else %7B%0A                    Pair%3CDouble, Double%3E p = new Pair%3C%3E(x, function.call(x));%0A                    iter.previous();%0A                    iter.add(series%5Bi%5D = p);%0A                    break;%0A                %7D%0A            %7D%0A        %7D%0A%0A        seriesCache.addFirst(new Pair%3CDouble,Double%3E(Double.NEGATIVE_INFINITY, null))
 ;%0A  
@@ -2157,32 +2157,34 @@
 ndex) %7B%0A        
+//
 return lowerBoun
@@ -2201,16 +2201,52 @@
  index;%0A
+        return series%5Bindex%5D.first;%0A
     %7D%0A%0A 
@@ -2298,24 +2298,26 @@
 ) %7B%0A        
+//
 return funct
@@ -2349,24 +2349,61 @@
 leValue());%0A
+        return series%5Bindex%5D.second;%0A
     %7D%0A%0A    @

Implement cache for FunctionWrapper