Skip to content

Instantly share code, notes, and snippets.

@Vogel612
Created August 14, 2015 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vogel612/774e4a727cec1350f6ae to your computer and use it in GitHub Desktop.
Save Vogel612/774e4a727cec1350f6ae to your computer and use it in GitHub Desktop.
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