Auto reformat classes
This commit is contained in:
parent
fd92f5e807
commit
a503362c36
@ -51,7 +51,8 @@ public class CalculateAverage_ebarlas {
|
|||||||
try {
|
try {
|
||||||
var buffer = channel.map(FileChannel.MapMode.READ_ONLY, pStart, pSize);
|
var buffer = channel.map(FileChannel.MapMode.READ_ONLY, pStart, pSize);
|
||||||
partitions[pIdx] = processBuffer(buffer, pIdx == 0);
|
partitions[pIdx] = processBuffer(buffer, pIdx == 0);
|
||||||
} catch (IOException e) {
|
}
|
||||||
|
catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -94,7 +95,8 @@ public class CalculateAverage_ebarlas {
|
|||||||
var t = target[j];
|
var t = target[j];
|
||||||
if (t == null) {
|
if (t == null) {
|
||||||
target[j] = current[j]; // copy ref from current to target
|
target[j] = current[j]; // copy ref from current to target
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
t.min = Math.min(t.min, current[j].min);
|
t.min = Math.min(t.min, current[j].min);
|
||||||
t.max = Math.max(t.max, current[j].max);
|
t.max = Math.max(t.max, current[j].max);
|
||||||
t.sum += current[j].sum;
|
t.sum += current[j].sum;
|
||||||
@ -154,10 +156,12 @@ public class CalculateAverage_ebarlas {
|
|||||||
st = stats[idx] = new Stats(key);
|
st = stats[idx] = new Stats(key);
|
||||||
}
|
}
|
||||||
readingKey = false;
|
readingKey = false;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
keyHash = HASH_FACTOR * keyHash + b;
|
keyHash = HASH_FACTOR * keyHash + b;
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
if (b == '\n') {
|
if (b == '\n') {
|
||||||
var v = negative ? -val : val;
|
var v = negative ? -val : val;
|
||||||
st.min = Math.min(st.min, v);
|
st.min = Math.min(st.min, v);
|
||||||
@ -169,9 +173,11 @@ public class CalculateAverage_ebarlas {
|
|||||||
val = 0;
|
val = 0;
|
||||||
negative = false;
|
negative = false;
|
||||||
keyStart = buffer.position();
|
keyStart = buffer.position();
|
||||||
} else if (b == '-') {
|
}
|
||||||
|
else if (b == '-') {
|
||||||
negative = true;
|
negative = true;
|
||||||
} else if (b != '.') { // skip '.' since fractional tenth unit after decimal point is assumed
|
}
|
||||||
|
else if (b != '.') { // skip '.' since fractional tenth unit after decimal point is assumed
|
||||||
val = val * 10 + (b - '0');
|
val = val * 10 + (b - '0');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -187,13 +193,15 @@ public class CalculateAverage_ebarlas {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] readHeader(ByteBuffer buffer) { // read up to and including first newline (or end-of-input)
|
private static byte[] readHeader(ByteBuffer buffer) { // read up to and including first newline (or end-of-input)
|
||||||
while (buffer.hasRemaining() && buffer.get() != '\n') ;
|
while (buffer.hasRemaining() && buffer.get() != '\n')
|
||||||
|
;
|
||||||
var header = new byte[buffer.position()];
|
var header = new byte[buffer.position()];
|
||||||
buffer.get(0, header, 0, header.length);
|
buffer.get(0, header, 0, header.length);
|
||||||
return header;
|
return header;
|
||||||
}
|
}
|
||||||
|
|
||||||
record Partition(byte[] header, byte[] footer, Stats[] stats) {}
|
record Partition(byte[] header, byte[] footer, Stats[] stats) {
|
||||||
|
}
|
||||||
|
|
||||||
private static class Stats { // min, max, and sum values are modeled with integral types that represent tenths of a unit
|
private static class Stats { // min, max, and sum values are modeled with integral types that represent tenths of a unit
|
||||||
final byte[] key;
|
final byte[] key;
|
||||||
|
@ -26,7 +26,6 @@ public class CalculateAverage_seijikun {
|
|||||||
|
|
||||||
private static final String FILE = "./measurements.txt";
|
private static final String FILE = "./measurements.txt";
|
||||||
|
|
||||||
|
|
||||||
private static class MeasurementAggregator {
|
private static class MeasurementAggregator {
|
||||||
private double min = Double.POSITIVE_INFINITY;
|
private double min = Double.POSITIVE_INFINITY;
|
||||||
private double max = Double.NEGATIVE_INFINITY;
|
private double max = Double.NEGATIVE_INFINITY;
|
||||||
@ -179,7 +178,7 @@ public class CalculateAverage_seijikun {
|
|||||||
chunkStartPtr = chunkEndPtr;
|
chunkStartPtr = chunkEndPtr;
|
||||||
}
|
}
|
||||||
|
|
||||||
try(var executor = Executors.newFixedThreadPool(jobCnt)) {
|
try (var executor = Executors.newFixedThreadPool(jobCnt)) {
|
||||||
for (int i = 0; i < jobCnt; ++i) {
|
for (int i = 0; i < jobCnt; ++i) {
|
||||||
executor.submit(chunks[i]);
|
executor.submit(chunks[i]);
|
||||||
}
|
}
|
||||||
|
@ -105,9 +105,8 @@ public class CalculateAverage_truelive {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static Map<String, Measurement> combineMaps(
|
private static Map<String, Measurement> combineMaps(
|
||||||
final Map<String, Measurement> map1,
|
final Map<String, Measurement> map1,
|
||||||
final Map<String, Measurement> map2
|
final Map<String, Measurement> map2) {
|
||||||
) {
|
|
||||||
for (final var entry : map2.entrySet()) {
|
for (final var entry : map2.entrySet()) {
|
||||||
map1.merge(entry.getKey(), entry.getValue(), Measurement::combineWith);
|
map1.merge(entry.getKey(), entry.getValue(), Measurement::combineWith);
|
||||||
}
|
}
|
||||||
@ -116,7 +115,7 @@ public class CalculateAverage_truelive {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(final String[] args) throws IOException {
|
public static void main(final String[] args) throws IOException {
|
||||||
//long before = System.currentTimeMillis();
|
// long before = System.currentTimeMillis();
|
||||||
/**
|
/**
|
||||||
* Shoutout to bjhara
|
* Shoutout to bjhara
|
||||||
*/
|
*/
|
||||||
@ -136,8 +135,7 @@ public class CalculateAverage_truelive {
|
|||||||
final MappedByteBuffer mbb = in.map(
|
final MappedByteBuffer mbb = in.map(
|
||||||
FileChannel.MapMode.READ_ONLY,
|
FileChannel.MapMode.READ_ONLY,
|
||||||
start,
|
start,
|
||||||
Math.min(CHUNK_SIZE, total - start)
|
Math.min(CHUNK_SIZE, total - start));
|
||||||
);
|
|
||||||
int realEnd = mbb.limit() - 1;
|
int realEnd = mbb.limit() - 1;
|
||||||
while (mbb.get(realEnd) != '\n') {
|
while (mbb.get(realEnd) != '\n') {
|
||||||
realEnd--;
|
realEnd--;
|
||||||
@ -148,16 +146,17 @@ public class CalculateAverage_truelive {
|
|||||||
start += realEnd;
|
start += realEnd;
|
||||||
|
|
||||||
return mbb;
|
return mbb;
|
||||||
} catch (final IOException e) {
|
}
|
||||||
|
catch (final IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
final Map<String, Measurement> reduce = StreamSupport.stream(Spliterators.spliteratorUnknownSize(
|
final Map<String, Measurement> reduce = StreamSupport.stream(Spliterators.spliteratorUnknownSize(
|
||||||
iterator, Spliterator.IMMUTABLE), true)
|
iterator, Spliterator.IMMUTABLE), true)
|
||||||
.parallel()
|
.parallel()
|
||||||
.map(CalculateAverage_truelive::parseBuffer)
|
.map(CalculateAverage_truelive::parseBuffer)
|
||||||
.reduce(CalculateAverage_truelive::combineMaps).get();
|
.reduce(CalculateAverage_truelive::combineMaps).get();
|
||||||
|
|
||||||
System.out.print("{");
|
System.out.print("{");
|
||||||
System.out.print(
|
System.out.print(
|
||||||
@ -169,7 +168,7 @@ public class CalculateAverage_truelive {
|
|||||||
.collect(Collectors.joining(", ")));
|
.collect(Collectors.joining(", ")));
|
||||||
System.out.println("}");
|
System.out.println("}");
|
||||||
|
|
||||||
//System.out.println("Took: " + (System.currentTimeMillis() - before));
|
// System.out.println("Took: " + (System.currentTimeMillis() - before));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -189,7 +188,8 @@ public class CalculateAverage_truelive {
|
|||||||
name = new String(arr, 0, len);
|
name = new String(arr, 0, len);
|
||||||
bug.position(pos);
|
bug.position(pos);
|
||||||
bug.mark();
|
bug.mark();
|
||||||
} else if (c == '\n') {
|
}
|
||||||
|
else if (c == '\n') {
|
||||||
final int pos = bug.position();
|
final int pos = bug.position();
|
||||||
bug.reset();
|
bug.reset();
|
||||||
final int len = pos - bug.position();
|
final int len = pos - bug.position();
|
||||||
|
@ -35,7 +35,7 @@ public class CreateMeasurements2 {
|
|||||||
private static final String FILE = "./measurements2.txt";
|
private static final String FILE = "./measurements2.txt";
|
||||||
|
|
||||||
static class WeatherStation {
|
static class WeatherStation {
|
||||||
final static char[] NUMBERS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
|
final static char[] NUMBERS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
|
||||||
|
|
||||||
final String id;
|
final String id;
|
||||||
final int meanTemperature;
|
final int meanTemperature;
|
||||||
@ -58,8 +58,8 @@ public class CreateMeasurements2 {
|
|||||||
* @param buffer the buffer to append to
|
* @param buffer the buffer to append to
|
||||||
*/
|
*/
|
||||||
void measurement(final CheaperCharBuffer buffer) {
|
void measurement(final CheaperCharBuffer buffer) {
|
||||||
// fake -10.9 to +10.9 variance without double operations and rounding
|
// fake -10.9 to +10.9 variance without double operations and rounding
|
||||||
// gives us -10 to +10
|
// gives us -10 to +10
|
||||||
int m = meanTemperature + (r.nextInt(21) - 10);
|
int m = meanTemperature + (r.nextInt(21) - 10);
|
||||||
// gives us a decimal digit 0 to 9 as char
|
// gives us a decimal digit 0 to 9 as char
|
||||||
char d = NUMBERS[r.nextInt(10)];
|
char d = NUMBERS[r.nextInt(10)];
|
||||||
@ -569,39 +569,39 @@ public class CreateMeasurements2 {
|
|||||||
|
|
||||||
// manual loop unroll for less jumps
|
// manual loop unroll for less jumps
|
||||||
for (int i = 0; i < count; i = i + 8) {
|
for (int i = 0; i < count; i = i + 8) {
|
||||||
{
|
{
|
||||||
// try to fill teh cpu pipeline as much as possible with
|
// try to fill teh cpu pipeline as much as possible with
|
||||||
// independent operations
|
// independent operations
|
||||||
int s1 = r1.nextInt(stationCount);
|
int s1 = r1.nextInt(stationCount);
|
||||||
int s2 = r2.nextInt(stationCount);
|
int s2 = r2.nextInt(stationCount);
|
||||||
int s3 = r3.nextInt(stationCount);
|
int s3 = r3.nextInt(stationCount);
|
||||||
int s4 = r4.nextInt(stationCount);
|
int s4 = r4.nextInt(stationCount);
|
||||||
// get us the ojects one after the other to have the array
|
// get us the ojects one after the other to have the array
|
||||||
// in our L1 cache and not push it out with other data
|
// in our L1 cache and not push it out with other data
|
||||||
var w1 = stations.get(s1);
|
var w1 = stations.get(s1);
|
||||||
var w2 = stations.get(s2);
|
var w2 = stations.get(s2);
|
||||||
var w3 = stations.get(s3);
|
var w3 = stations.get(s3);
|
||||||
var w4 = stations.get(s4);
|
var w4 = stations.get(s4);
|
||||||
// write our data to our buffer
|
// write our data to our buffer
|
||||||
w1.measurement(sb);
|
w1.measurement(sb);
|
||||||
w2.measurement(sb);
|
w2.measurement(sb);
|
||||||
w3.measurement(sb);
|
w3.measurement(sb);
|
||||||
w4.measurement(sb);
|
w4.measurement(sb);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
int s1 = r1.nextInt(stationCount);
|
int s1 = r1.nextInt(stationCount);
|
||||||
int s2 = r2.nextInt(stationCount);
|
int s2 = r2.nextInt(stationCount);
|
||||||
int s3 = r3.nextInt(stationCount);
|
int s3 = r3.nextInt(stationCount);
|
||||||
int s4 = r4.nextInt(stationCount);
|
int s4 = r4.nextInt(stationCount);
|
||||||
var w1 = stations.get(s1);
|
var w1 = stations.get(s1);
|
||||||
var w2 = stations.get(s2);
|
var w2 = stations.get(s2);
|
||||||
var w3 = stations.get(s3);
|
var w3 = stations.get(s3);
|
||||||
var w4 = stations.get(s4);
|
var w4 = stations.get(s4);
|
||||||
w1.measurement(sb);
|
w1.measurement(sb);
|
||||||
w2.measurement(sb);
|
w2.measurement(sb);
|
||||||
w3.measurement(sb);
|
w3.measurement(sb);
|
||||||
w4.measurement(sb);
|
w4.measurement(sb);
|
||||||
}
|
}
|
||||||
// write the buffer directly, no intermediate string copy
|
// write the buffer directly, no intermediate string copy
|
||||||
bw.write(sb.data_, 0, sb.length_);
|
bw.write(sb.data_, 0, sb.length_);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user