Skip to content

Commit

Permalink
Use Math.clamp to improve code readability
Browse files Browse the repository at this point in the history
  • Loading branch information
wendigo authored and dain committed Sep 8, 2024
1 parent 019b658 commit befc81b
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static io.airlift.compress.v3.lz4.Lz4Constants.SIZE_OF_LONG;
import static io.airlift.compress.v3.lz4.Lz4Constants.SIZE_OF_SHORT;
import static io.airlift.compress.v3.lz4.UnsafeUtil.UNSAFE;
import static java.lang.Math.clamp;

final class Lz4RawCompressor
{
Expand Down Expand Up @@ -306,6 +307,6 @@ static int computeTableSize(int inputSize)
int target = Integer.highestOneBit(inputSize - 1) << 1;

// keep it between MIN_TABLE_SIZE and MAX_TABLE_SIZE
return Math.max(Math.min(target, MAX_TABLE_SIZE), MIN_TABLE_SIZE);
return clamp(target, MIN_TABLE_SIZE, MAX_TABLE_SIZE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static io.airlift.compress.v3.lzo.LzoConstants.SIZE_OF_LONG;
import static io.airlift.compress.v3.lzo.LzoConstants.SIZE_OF_SHORT;
import static io.airlift.compress.v3.lzo.UnsafeUtil.UNSAFE;
import static java.lang.Math.clamp;

final class LzoRawCompressor
{
Expand Down Expand Up @@ -384,6 +385,6 @@ private static int computeTableSize(int inputSize)
int target = Integer.highestOneBit(inputSize - 1) << 1;

// keep it between MIN_TABLE_SIZE and MAX_TABLE_SIZE
return Math.max(Math.min(target, MAX_TABLE_SIZE), MIN_TABLE_SIZE);
return clamp(target, MIN_TABLE_SIZE, MAX_TABLE_SIZE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static io.airlift.compress.v3.snappy.SnappyConstants.SIZE_OF_LONG;
import static io.airlift.compress.v3.snappy.SnappyConstants.SIZE_OF_SHORT;
import static io.airlift.compress.v3.snappy.UnsafeUtil.UNSAFE;
import static java.lang.Math.clamp;

final class SnappyRawCompressor
{
Expand Down Expand Up @@ -356,7 +357,7 @@ static int getHashTableSize(int inputSize)
int target = Integer.highestOneBit(inputSize - 1) << 1;

// keep it between MIN_TABLE_SIZE and MAX_TABLE_SIZE
return Math.max(Math.min(target, MAX_HASH_TABLE_SIZE), 256);
return clamp(target, 256, MAX_HASH_TABLE_SIZE);
}

// Any hash function will produce a valid compressed stream, but a good
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import static io.airlift.compress.v3.zstd.Constants.MAX_BLOCK_SIZE;
import static io.airlift.compress.v3.zstd.Util.checkArgument;
import static java.lang.Math.clamp;

class CompressionContext
{
Expand All @@ -31,7 +32,7 @@ public CompressionContext(CompressionParameters parameters, long baseAddress, in
{
this.parameters = parameters;

int windowSize = Math.max(1, Math.min(parameters.getWindowSize(), inputSize));
int windowSize = clamp(inputSize, 1, parameters.getWindowSize());
int blockSize = Math.min(MAX_BLOCK_SIZE, windowSize);
int divider = (parameters.getSearchLength() == 3) ? 3 : 4;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static io.airlift.compress.v3.zstd.Constants.MIN_WINDOW_LOG;
import static io.airlift.compress.v3.zstd.Util.cycleLog;
import static io.airlift.compress.v3.zstd.Util.highestBit;
import static java.lang.Math.clamp;

class CompressionParameters
{
Expand Down Expand Up @@ -316,7 +317,7 @@ else if (estimatedInputSize <= 256 * 1024) {
int row = DEFAULT_COMPRESSION_LEVEL;

if (compressionLevel != 0) { // TODO: figure out better way to indicate default compression level
row = Math.min(Math.max(0, compressionLevel), MAX_COMPRESSION_LEVEL);
row = clamp(compressionLevel, 0, MAX_COMPRESSION_LEVEL);
}

return DEFAULT_COMPRESSION_PARAMETERS[table][row];
Expand Down

0 comments on commit befc81b

Please sign in to comment.