Skip to content

Instantly share code, notes, and snippets.

@Vogel612

Vogel612/Main.java Secret

Created Aug 14, 2015
Embed
What would you like to do?
package de.vogel612.codereview.r2r.mediansorting;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public final class Main {
public static void main(final String[] args) {
Path filename = Paths.get(args[0]);
final int slidingWindowSize;
if (args.length == 2) {
slidingWindowSize = Integer.parseInt(args[1]);
} else {
slidingWindowSize = 5;
}
Comparable[] data = readFile(filename);
MedianFilter filtering = new MedianFilter(data, slidingWindowSize);
filtering.doFilter();
filename = filename.resolveSibling(args[0] + ".out");
writeToFile(filename, filtering.getResults());
}
private static void writeToFile(final Path filename,
final Comparable[] results) {
try (FileWriter out = new FileWriter(filename.toFile())) {
Arrays.stream(results)
.map(r -> String.valueOf(r) + System.lineSeparator())
.forEach(l -> {
try {
out.append(l);
} catch (IOException e) {
// not giving a fuck
e.printStackTrace(System.err);
}
});
} catch (IOException e) {
e.printStackTrace(System.err);
// fudge this
throw new UncheckedIOException(e);
}
}
private static Comparable[] readFile(final Path sourceFile) {
try (Stream<String> lines = Files.lines(sourceFile)) {
// we assume integers for now
return lines.map(Integer::parseInt).collect(Collectors.toList())
.toArray(new Comparable[]{});
} catch (IOException e) {
e.printStackTrace(System.err);
throw new UncheckedIOException(e);
}
}
}
package de.vogel612.codereview.r2r.mediansorting;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
public class MedianFilter {
private final CountDownLatch completionLatch = new CountDownLatch(1);
private final Comparable[] sourceArray;
private final Comparable[] targetArray;
private final int slidingWindowSize;
public MedianFilter(final Comparable[] data, final int windowSize) {
// we can assume that windowSize is an odd integer (3, 21)
assert (windowSize % 2 == 1);
if (windowSize > data.length) {
throw new IllegalArgumentException(
"sliding window size cannot exceed array length");
}
sourceArray = data;
targetArray = new Comparable[data.length];
slidingWindowSize = windowSize;
}
public Comparable[] getResults() {
// check that we really completed :)
try {
completionLatch.await();
} catch (InterruptedException e) {
// interruption while waiting, should never happen here, so we
// ignore that
}
return targetArray;
}
public void doFilter() {
final int shoulderSize = (slidingWindowSize - 1) / 2;
final int startIndex = shoulderSize;
final int endIndex = sourceArray.length - shoulderSize;
// copy over shoulders
System.arraycopy(sourceArray, 0, targetArray, 0, shoulderSize);
System.arraycopy(sourceArray, endIndex, targetArray, endIndex,
shoulderSize);
for (int index = startIndex; index < endIndex; index++) {
runMedian(index, shoulderSize);
}
completionLatch.countDown();
}
private void runMedian(final int index, final int shoulderSize) {
Comparable[] window = new Comparable[slidingWindowSize];
System.arraycopy(sourceArray, index - shoulderSize, window, 0,
slidingWindowSize);
Arrays.sort(window, null); // use natural ordering of Comparable
targetArray[index] = window[shoulderSize];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment