Skip to content

Commit

Permalink
Update cheesy libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
rakrakon committed Feb 8, 2024
1 parent 74eea28 commit 9585679
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 17 deletions.
1 change: 0 additions & 1 deletion src/main/java/lib/math/interpolation/Interpolable.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* @see InterpolatingTreeMap
*/
public interface Interpolable<T> {

/**
* 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
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/lib/math/interpolation/InterpolatingDouble.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,40 @@ public class InterpolatingDouble
implements Interpolable<InterpolatingDouble>,
InverseInterpolable<InterpolatingDouble>,
Comparable<InterpolatingDouble> {
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;
}
}
}
48 changes: 48 additions & 0 deletions src/main/java/lib/math/interpolation/InterpolatingLong.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package lib.math.interpolation;

/**
* A Long that can be interpolated using the InterpolatingTreeMap.
*
* @see InterpolatingTreeMap
*/
public class InterpolatingLong
implements Interpolable<InterpolatingLong>,
InverseInterpolable<InterpolatingLong>,
Comparable<InterpolatingLong> {
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
public class InterpolatingTreeMap<
K extends InverseInterpolable<K> & Comparable<K>, V extends Interpolable<V>>
extends TreeMap<K, V> {

private static final long serialVersionUID = 8347275262778054124L;

final int max;
final int max_;

public InterpolatingTreeMap(int maximumSize) {
max = maximumSize;
max_ = maximumSize;
}

public InterpolatingTreeMap() {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
* @see InterpolatingTreeMap
*/
public interface InverseInterpolable<T> {

/**
* 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.
*/
Expand Down

0 comments on commit 9585679

Please sign in to comment.