From 9585679154adb4ae47c059c526c0a38d8d209d3f Mon Sep 17 00:00:00 2001 From: rakrakon <68773850+rakrakon@users.noreply.github.com> Date: Thu, 8 Feb 2024 16:44:54 +0200 Subject: [PATCH] Update cheesy libraries --- .../lib/math/interpolation/Interpolable.java | 1 - .../interpolation/InterpolatingDouble.java | 28 ++++++----- .../math/interpolation/InterpolatingLong.java | 48 +++++++++++++++++++ .../interpolation/InterpolatingTreeMap.java | 7 ++- .../interpolation/InverseInterpolable.java | 3 +- 5 files changed, 70 insertions(+), 17 deletions(-) create mode 100644 src/main/java/lib/math/interpolation/InterpolatingLong.java diff --git a/src/main/java/lib/math/interpolation/Interpolable.java b/src/main/java/lib/math/interpolation/Interpolable.java index ff934fcd..42b0a2df 100644 --- a/src/main/java/lib/math/interpolation/Interpolable.java +++ b/src/main/java/lib/math/interpolation/Interpolable.java @@ -9,7 +9,6 @@ * @see InterpolatingTreeMap */ public interface Interpolable { - /** * Interpolates between this value and an other value according to a given parameter. If x is 0, * the method should return this value. If x is 1, the method should return the other value. If diff --git a/src/main/java/lib/math/interpolation/InterpolatingDouble.java b/src/main/java/lib/math/interpolation/InterpolatingDouble.java index 1c528eab..0f2f6784 100644 --- a/src/main/java/lib/math/interpolation/InterpolatingDouble.java +++ b/src/main/java/lib/math/interpolation/InterpolatingDouble.java @@ -9,34 +9,40 @@ public class InterpolatingDouble implements Interpolable, InverseInterpolable, Comparable { + public Double value = 0.0; - public double value; - - public InterpolatingDouble(double val) { + public InterpolatingDouble(Double val) { value = val; } @Override public InterpolatingDouble interpolate(InterpolatingDouble other, double x) { - double dy = other.value - this.value; - return new InterpolatingDouble(dy * x + value); + Double dydx = other.value - value; + Double searchY = dydx * x + value; + return new InterpolatingDouble(searchY); } @Override public double inverseInterpolate(InterpolatingDouble upper, InterpolatingDouble query) { - double upperToLower = upper.value - value; - if (upperToLower <= 0) { + double upper_to_lower = upper.value - value; + if (upper_to_lower <= 0) { return 0; } - double queryToLower = query.value - value; - if (queryToLower <= 0) { + double query_to_lower = query.value - value; + if (query_to_lower <= 0) { return 0; } - return queryToLower / upperToLower; + return query_to_lower / upper_to_lower; } @Override public int compareTo(InterpolatingDouble other) { - return Double.compare(value, other.value); + if (other.value < value) { + return 1; + } else if (other.value > value) { + return -1; + } else { + return 0; + } } } diff --git a/src/main/java/lib/math/interpolation/InterpolatingLong.java b/src/main/java/lib/math/interpolation/InterpolatingLong.java new file mode 100644 index 00000000..483341ff --- /dev/null +++ b/src/main/java/lib/math/interpolation/InterpolatingLong.java @@ -0,0 +1,48 @@ +package lib.math.interpolation; + +/** + * A Long that can be interpolated using the InterpolatingTreeMap. + * + * @see InterpolatingTreeMap + */ +public class InterpolatingLong + implements Interpolable, + InverseInterpolable, + Comparable { + public Long value = 0L; + + public InterpolatingLong(Long val) { + value = val; + } + + @Override + public InterpolatingLong interpolate(InterpolatingLong other, double x) { + Long dydx = other.value - value; + Double searchY = dydx * x + value; + return new InterpolatingLong(searchY.longValue()); + } + + @Override + public double inverseInterpolate(InterpolatingLong upper, InterpolatingLong query) { + long upper_to_lower = upper.value - value; + if (upper_to_lower <= 0) { + return 0; + } + long query_to_lower = query.value - value; + if (query_to_lower <= 0) { + return 0; + } + return query_to_lower / (double) upper_to_lower; + } + + @Override + public int compareTo(InterpolatingLong other) { + if (other.value < value) { + return 1; + } else if (other.value > value) { + return -1; + } else { + return 0; + } + } +} diff --git a/src/main/java/lib/math/interpolation/InterpolatingTreeMap.java b/src/main/java/lib/math/interpolation/InterpolatingTreeMap.java index 45252633..a24e4851 100644 --- a/src/main/java/lib/math/interpolation/InterpolatingTreeMap.java +++ b/src/main/java/lib/math/interpolation/InterpolatingTreeMap.java @@ -13,13 +13,12 @@ public class InterpolatingTreeMap< K extends InverseInterpolable & Comparable, V extends Interpolable> extends TreeMap { - private static final long serialVersionUID = 8347275262778054124L; - final int max; + final int max_; public InterpolatingTreeMap(int maximumSize) { - max = maximumSize; + max_ = maximumSize; } public InterpolatingTreeMap() { @@ -35,7 +34,7 @@ public InterpolatingTreeMap() { */ @Override public V put(K key, V value) { - if (max > 0 && max <= size()) { + if (max_ > 0 && max_ <= size()) { // "Prune" the tree if it is oversize K first = firstKey(); remove(first); diff --git a/src/main/java/lib/math/interpolation/InverseInterpolable.java b/src/main/java/lib/math/interpolation/InverseInterpolable.java index 0deb7695..23de4a0b 100644 --- a/src/main/java/lib/math/interpolation/InverseInterpolable.java +++ b/src/main/java/lib/math/interpolation/InverseInterpolable.java @@ -9,11 +9,12 @@ * @see InterpolatingTreeMap */ public interface InverseInterpolable { - /** * Given this point (lower), a query point (query), and an upper point (upper), estimate how far * (on [0, 1]) between 'lower' and 'upper' the query point lies. * + * @param upper + * @param query * @return The interpolation parameter on [0, 1] representing how far between this point and the * upper point the query point lies. */