Skip to content

Commit

Permalink
Math.clamp
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry committed Oct 26, 2023
1 parent b108f88 commit e461fcd
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private static int guessPendingChunkLimit(long chunkSize) {
int minChunks = 1;
int maxChunks = (int) (MAX_BYTES_TO_USE / chunkSize);
int targetChunks = (int) (ProcessInfo.getMaxMemoryBytes() * 0.5d / chunkSize);
return Math.min(maxChunks, Math.max(minChunks, targetChunks));
return Math.clamp(targetChunks, minChunks, maxChunks);
}

public void init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public static Geometry createGeometryCollection(List<Geometry> polygonGroup) {
/** Returns a point approximately {@code ratio} of the way from start to end and {@code offset} units to the right. */
public static Point pointAlongOffset(LineString lineString, double ratio, double offset) {
int numPoints = lineString.getNumPoints();
int middle = Math.max(0, Math.min(numPoints - 2, (int) (numPoints * ratio)));
int middle = Math.clamp((int) (numPoints * ratio), 0, numPoints - 2);
Coordinate a = lineString.getCoordinateN(middle);
Coordinate b = lineString.getCoordinateN(middle + 1);
LineSegment segment = new LineSegment(a, b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ private TileExtents(ForZoom[] zoomExtents) {
}

private static int quantizeDown(double value, int levels) {
return Math.max(0, Math.min(levels, (int) Math.floor(value * levels)));
return Math.clamp((int) Math.floor(value * levels), 0, levels);
}

private static int quantizeUp(double value, int levels) {
return Math.max(0, Math.min(levels, (int) Math.ceil(value * levels)));
return Math.clamp((int) Math.ceil(value * levels), 0, levels);
}

/** Returns a filter to tiles that intersect {@code worldBounds} (specified in world web mercator coordinates). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* To sort by a field descending, specify its range from high to low.
* <p>
* For example this SQL ordering:
*
*
* <pre>
* {@code
* ORDER BY rank ASC,
Expand All @@ -23,7 +23,7 @@
* </pre>
* <p>
* would become:
*
*
* <pre>
* {@code
* feature.setSortKey(
Expand Down Expand Up @@ -125,7 +125,7 @@ public SortKey thenByInt(int value, int start, int end) {
}
int levels = end + 1 - start;
if (value < start || value > end) {
value = Math.max(start, Math.min(end, value));
value = Math.clamp(value, start, end);
}
return accumulate(value, start, levels);
}
Expand All @@ -141,7 +141,7 @@ public SortKey thenByDouble(double value, double start, double end, int levels)
return thenByDouble(start - value, end, start, levels);
}
if (value < start || value > end) {
value = Math.max(start, Math.min(end, value));
value = Math.clamp(value, start, end);
}

int intVal = doubleRangeToInt(value, start, end, levels);
Expand All @@ -160,7 +160,7 @@ public SortKey thenByLog(double value, double start, double end, int levels) {
}
assert start > 0 : "log thresholds must be > 0 got [" + start + ", " + end + "]";
if (value < start || value > end) {
value = Math.max(start, Math.min(end, value));
value = Math.clamp(value, start, end);
}

int intVal = doubleRangeToInt(Math.log(value), Math.log(start), Math.log(end), levels);
Expand Down

0 comments on commit e461fcd

Please sign in to comment.