Constraint caches and digest caching made more efficient.

v5stable
Ondřej Hruška 11 years ago
parent eb981454c0
commit 0fa85dc371
  1. 2
      src/mightypork/rogue/screens/test_bouncyboxes/BouncyBox.java
  2. 2
      src/mightypork/rogue/screens/test_cat_sound/LayerFlyingCat.java
  3. 39
      src/mightypork/test/TestConstCaching.java
  4. 34
      src/mightypork/utils/math/constraints/num/Num.java
  5. 2
      src/mightypork/utils/math/constraints/num/NumAnimatedDeg.java
  6. 2
      src/mightypork/utils/math/constraints/num/NumAnimatedRad.java
  7. 1
      src/mightypork/utils/math/constraints/num/NumBound.java
  8. 2
      src/mightypork/utils/math/constraints/num/NumBoundAdapter.java
  9. 37
      src/mightypork/utils/math/constraints/num/NumCache.java
  10. 20
      src/mightypork/utils/math/constraints/num/NumProxy.java
  11. 2
      src/mightypork/utils/math/constraints/num/PluggableNumBound.java
  12. 30
      src/mightypork/utils/math/constraints/rect/Rect.java
  13. 2
      src/mightypork/utils/math/constraints/rect/RectBound.java
  14. 2
      src/mightypork/utils/math/constraints/rect/RectBoundAdapter.java
  15. 37
      src/mightypork/utils/math/constraints/rect/RectCache.java
  16. 2
      src/mightypork/utils/math/constraints/rect/RectDigest.java
  17. 33
      src/mightypork/utils/math/constraints/vect/Vect.java
  18. 2
      src/mightypork/utils/math/constraints/vect/VectBound.java
  19. 2
      src/mightypork/utils/math/constraints/vect/VectBoundAdapter.java
  20. 37
      src/mightypork/utils/math/constraints/vect/VectCache.java
  21. 6
      src/mightypork/utils/math/constraints/vect/VectConst.java
  22. 20
      src/mightypork/utils/math/constraints/vect/VectProxy.java

@ -9,8 +9,8 @@ import mightypork.gamecore.gui.components.SimplePainter;
import mightypork.gamecore.render.Render;
import mightypork.utils.math.Easing;
import mightypork.utils.math.color.RGB;
import mightypork.utils.math.constraints.num.NumAnimated;
import mightypork.utils.math.constraints.num.Num;
import mightypork.utils.math.constraints.num.NumAnimated;
import mightypork.utils.math.constraints.rect.Rect;

@ -16,8 +16,8 @@ import mightypork.gamecore.render.fonts.FontRenderer.Align;
import mightypork.rogue.Res;
import mightypork.utils.math.Easing;
import mightypork.utils.math.color.RGB;
import mightypork.utils.math.constraints.num.NumAnimated;
import mightypork.utils.math.constraints.num.Num;
import mightypork.utils.math.constraints.num.NumAnimated;
import mightypork.utils.math.constraints.rect.Rect;
import mightypork.utils.math.constraints.vect.Vect;
import mightypork.utils.math.constraints.vect.VectAnimated;

@ -0,0 +1,39 @@
package mightypork.test;
import mightypork.utils.math.constraints.vect.Vect;
import mightypork.utils.math.constraints.vect.VectCache;
import mightypork.utils.math.constraints.vect.VectVar;
public class TestConstCaching {
public static void main(String[] args)
{
VectVar in = Vect.makeVar(0, 0);
VectCache cache = in.cached();
cache.enableDigestCaching(true);
System.out.println("in = "+in);
System.out.println("cache = "+cache);
System.out.println("cache digest = " + cache.digest());
System.out.println("\n-- in := 100, 50, 25 --\n");
in.setTo(100,50,25);
System.out.println("in = "+in);
System.out.println("cache = "+cache);
System.out.println("cache digest = " + cache.digest());
System.out.println("\n-- cache.poll() --\n");
cache.poll();
System.out.println("in = "+in);
System.out.println("cache = "+cache);
System.out.println("cache digest = " + cache.digest());
System.out.println("\n-- in := 1, 2, 3 --\n");
in.setTo(1,2,3);
System.out.println("in = "+in);
System.out.println("cache = "+cache);
System.out.println("cache digest = " + cache.digest());
System.out.println("\n-- cache.poll() --\n");
cache.poll();
System.out.println("cache = "+cache);
System.out.println("cache digest = " + cache.digest());
}
}

@ -62,8 +62,9 @@ public abstract class Num implements NumBound, Digestable<NumDigest> {
private Num p_square;
private Num p_neg;
private Num p_abs;
private NumDigest lastDigest;
private boolean digestCachingEnabled;
private NumDigest lastDigest = null;
private boolean digestCachingEnabled = false;
private boolean digestDirty = true;
public NumConst freeze()
@ -72,6 +73,18 @@ public abstract class Num implements NumBound, Digestable<NumDigest> {
}
/**
* Wrap this constraint into a caching adapter. Value will stay fixed (ie.
* no re-calculations) until cache receives a poll() call.
*
* @return the caching adapter
*/
public NumCache cached()
{
return new NumCache(this);
}
/**
* Get a snapshot of the current state, to be used for processing.
*
@ -81,9 +94,16 @@ public abstract class Num implements NumBound, Digestable<NumDigest> {
@Override
public NumDigest digest()
{
if (digestCachingEnabled && lastDigest != null) return lastDigest;
if (digestCachingEnabled) {
if (digestDirty) {
lastDigest = new NumDigest(this);
digestDirty = false;
}
return lastDigest;
}
return lastDigest = new NumDigest(this);
return new NumDigest(this);
}
@ -91,12 +111,10 @@ public abstract class Num implements NumBound, Digestable<NumDigest> {
public void enableDigestCaching(boolean yes)
{
digestCachingEnabled = yes;
digestDirty = true; // schedule update nest time digest() is called
}
/**
* @return true if digest caching is enabled.
*/
@Override
public boolean isDigestCachingEnabled()
{
@ -107,7 +125,7 @@ public abstract class Num implements NumBound, Digestable<NumDigest> {
@Override
public void poll()
{
if (digestCachingEnabled) lastDigest = new NumDigest(this);
if (digestCachingEnabled) digestDirty = true;
}

@ -2,8 +2,8 @@ package mightypork.utils.math.constraints.num;
import mightypork.utils.math.Calc;
import mightypork.utils.math.Easing;
import mightypork.utils.math.Calc.Deg;
import mightypork.utils.math.Easing;
/**

@ -2,8 +2,8 @@ package mightypork.utils.math.constraints.num;
import mightypork.utils.math.Calc;
import mightypork.utils.math.Easing;
import mightypork.utils.math.Calc.Rad;
import mightypork.utils.math.Easing;
/**

@ -1,7 +1,6 @@
package mightypork.utils.math.constraints.num;
/**
* Numeric constraint
*

@ -1,8 +1,6 @@
package mightypork.utils.math.constraints.num;
public class NumBoundAdapter extends NumAdapter implements PluggableNumBound {
private NumBound backing = null;

@ -0,0 +1,37 @@
package mightypork.utils.math.constraints.num;
public class NumCache extends NumAdapter {
private final NumVar cache = Num.makeVar();
private final Num source;
private boolean inited = false;
public NumCache(Num source) {
this.source = source;
}
@Override
protected Num getSource()
{
if (!inited) poll();
return cache;
}
/**
* Update cached value and cached digest (if digest caching is enabled)
*/
@Override
public void poll()
{
inited = true;
cache.setTo(source);
super.poll();
}
}

@ -0,0 +1,20 @@
package mightypork.utils.math.constraints.num;
public class NumProxy extends NumAdapter {
private final Num source;
public NumProxy(Num source) {
this.source = source;
}
@Override
protected Num getSource()
{
return source;
}
}

@ -1,8 +1,6 @@
package mightypork.utils.math.constraints.num;
/**
* Pluggable numeric constraint
*

@ -161,6 +161,7 @@ public abstract class Rect implements RectBound, Digestable<RectDigest> {
private RectDigest lastDigest = null;
private boolean digestCachingEnabled = false;
private boolean digestDirty = true; // digest is null
/**
@ -175,12 +176,31 @@ public abstract class Rect implements RectBound, Digestable<RectDigest> {
}
/**
* Wrap this constraint into a caching adapter. Value will stay fixed (ie.
* no re-calculations) until cache receives a poll() call.
*
* @return the caching adapter
*/
public RectCache cached()
{
return new RectCache(this);
}
@Override
public RectDigest digest()
{
if (digestCachingEnabled && lastDigest != null) return lastDigest;
if (digestCachingEnabled) {
if (digestDirty) {
lastDigest = new RectDigest(this);
digestDirty = false;
}
return lastDigest;
}
return lastDigest = new RectDigest(this);
return new RectDigest(this);
}
@ -188,12 +208,10 @@ public abstract class Rect implements RectBound, Digestable<RectDigest> {
public void enableDigestCaching(boolean yes)
{
digestCachingEnabled = yes;
digestDirty = true; // schedule update nest time digest() is called
}
/**
* @return true if digest caching is enabled.
*/
@Override
public boolean isDigestCachingEnabled()
{
@ -204,7 +222,7 @@ public abstract class Rect implements RectBound, Digestable<RectDigest> {
@Override
public void poll()
{
if (digestCachingEnabled) lastDigest = new RectDigest(this);
if (digestCachingEnabled) digestDirty = true;
}

@ -1,8 +1,6 @@
package mightypork.utils.math.constraints.rect;
/**
* Rect constraint (ie. region)
*

@ -1,8 +1,6 @@
package mightypork.utils.math.constraints.rect;
/**
* Pluggable rect bound adapter
*

@ -0,0 +1,37 @@
package mightypork.utils.math.constraints.rect;
public class RectCache extends RectAdapter {
private final RectVar cache = Rect.makeVar();
private final Rect source;
private boolean inited = false;
public RectCache(Rect source) {
this.source = source;
}
@Override
protected Rect getSource()
{
if (!inited) poll();
return cache;
}
/**
* Update cached value and cached digest (if digest caching is enabled)
*/
@Override
public void poll()
{
inited = true;
cache.setTo(source);
super.poll();
}
}

@ -1,8 +1,6 @@
package mightypork.utils.math.constraints.rect;
public class RectDigest {
public final double x;

@ -104,8 +104,9 @@ public abstract class Vect implements VectBound, Digestable<VectDigest> {
private Num p_xc;
private Num p_yc;
private Num p_zc;
private boolean digestCachingEnabled;
private VectDigest lastDigest;
private VectDigest lastDigest = null;
private boolean digestCachingEnabled = false;
private boolean digestDirty = true; // it's null
/**
@ -238,12 +239,31 @@ public abstract class Vect implements VectBound, Digestable<VectDigest> {
}
/**
* Wrap this constraint into a caching adapter. Value will stay fixed (ie.
* no re-calculations) until cache receives a poll() call.
*
* @return the caching adapter
*/
public VectCache cached()
{
return new VectCache(this);
}
@Override
public VectDigest digest()
{
if (digestCachingEnabled && lastDigest != null) return lastDigest;
if (digestCachingEnabled) {
if (digestDirty) {
lastDigest = new VectDigest(this);
digestDirty = false;
}
return lastDigest;
}
return lastDigest = new VectDigest(this);
return new VectDigest(this);
}
@ -251,6 +271,7 @@ public abstract class Vect implements VectBound, Digestable<VectDigest> {
public void enableDigestCaching(boolean yes)
{
digestCachingEnabled = yes;
digestDirty = true; // schedule update nest time digest() is called
}
@ -264,7 +285,7 @@ public abstract class Vect implements VectBound, Digestable<VectDigest> {
@Override
public void poll()
{
if (digestCachingEnabled) lastDigest = new VectDigest(this);
if (digestCachingEnabled) digestDirty = true;
}
@ -1036,7 +1057,7 @@ public abstract class Vect implements VectBound, Digestable<VectDigest> {
* @param vec other vector
* @return result
*/
public final Vect cross(final Vect vec)
public Vect cross(final Vect vec)
{
return new Vect() {

@ -1,8 +1,6 @@
package mightypork.utils.math.constraints.vect;
/**
* Element holding a vector, used for constraint building.
*

@ -1,8 +1,6 @@
package mightypork.utils.math.constraints.vect;
public class VectBoundAdapter extends VectAdapter implements PluggableVectBound {
private VectBound backing = null;

@ -0,0 +1,37 @@
package mightypork.utils.math.constraints.vect;
public class VectCache extends VectAdapter {
private final VectVar cache = Vect.makeVar();
private final Vect source;
private boolean inited = false;
public VectCache(Vect source) {
this.source = source;
}
@Override
protected Vect getSource()
{
if (!inited) poll();
return cache;
}
/**
* Update cached value and cached digest (if digest caching is enabled)
*/
@Override
public void poll()
{
inited = true;
cache.setTo(source);
super.poll();
}
}

@ -351,6 +351,12 @@ public final class VectConst extends Vect {
}
public VectConst cross(VectConst vec)
{
return super.cross(vec).freeze();
}
@Override
public RectConst expand(double left, double right, double top, double bottom)
{

@ -0,0 +1,20 @@
package mightypork.utils.math.constraints.vect;
public class VectProxy extends VectAdapter {
private final Vect source;
public VectProxy(Vect source) {
this.source = source;
}
@Override
protected Vect getSource()
{
return source;
}
}
Loading…
Cancel
Save