Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

comments

  • Loading branch information...
commit 941ce36c3904b843e38d2d8efd47458fa585bc4e 1 parent 32b1d2f
David R. MacIver authored
Showing with 60 additions and 37 deletions.
  1. +12 −0 src/cooccurrences.java
  2. +48 −37 src/pearsons.java
View
12 src/cooccurrences.java
@@ -1,8 +1,20 @@
import java.io.*;
import java.lang.reflect.*;
+/**
+ * Little utility program as part of the larger pearsons code. Don't use
+ * this directly.
+ *
+ * Takes events of the form identifier1 ... identifiern and prints all pairs
+ * identifierj identifierk where identifierj < identifierk lexicographically
+ */
class cooccurrences{
public static void main(String[] args) throws Exception{
+ // We want to bypass the buffer on stdin as it gets buffered anyway and the
+ // superfluous buffering was knackering performance.
+ // Unfortunately the Java standard library gives us no way to do this.
+ // Or even, in fact, any indication that System.in is a BufferedInputStream.
+ // So we use reflection in amusingly abusibe ways to get access to the underlying
final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
final Field outout = FilterOutputStream.class.getDeclaredField("out");
outout.setAccessible(true);
View
85 src/pearsons.java
@@ -2,44 +2,14 @@
import java.io.*;
import java.lang.reflect.Field;
+/**
+ * Utility class for doing some of the heavy lifting for the larger pearsons
+ * program. Don't use this directly.
+ *
+ * Takes previously calculated tallies and sums and combines them to give the
+ * actual pearsons balues.
+ */
public class pearsons{
- public static void write(OutputStream writer, char c) throws Exception{
- writer.write((byte)c);
- }
-
- public static void write(OutputStream writer, String string) throws Exception{
- for(int i = 0; i < string.length(); i++){
- write(writer, string.charAt(i));
- }
- }
-
- public static void write(OutputStream writer, int x) throws Exception{
- if(x < 0 || x >= 100) {
- throw new RuntimeException(x + " out of bounds: My shitty int writing routine only handles ints in the range 0-99");
- }
-
- int tens = x / 10;
- x -= 10 * tens;
- int digits = x;
-
- writer.write('0' + tens);
- writer.write('0' + digits);
- }
-
- static void write(OutputStream writer, double x) throws Exception{
- if (x < 0){
- write(writer, '-');
- x = -x;
- }
-
- x = x + 0.005; // adjust for rounding
-
- int i = (int)x;
- write(writer, i);
- writer.write('.');
- write(writer, (int)(100 * (x - i)));
- }
-
public final static String[] FRAGMENTS = new String[3];
public static int firstSpace(String line){
@@ -120,4 +90,45 @@ public static void main(String[] args) throws Exception{
}
stdout.close();
}
+
+ // Specialized crufty hacks for writing things to output streams
+ // It's all gross and horrible and very special cased.
+ // It's also dramatically faster than the java.io.Reader infrastructure
+ // and the standard conversion routines to strings.
+ public static void write(OutputStream writer, char c) throws Exception{
+ writer.write((byte)c);
+ }
+
+ public static void write(OutputStream writer, String string) throws Exception{
+ for(int i = 0; i < string.length(); i++){
+ write(writer, string.charAt(i));
+ }
+ }
+
+ public static void write(OutputStream writer, int x) throws Exception{
+ if(x < 0 || x >= 100) {
+ throw new RuntimeException(x + " out of bounds: My shitty int writing routine only handles ints in the range 0-99");
+ }
+
+ int tens = x / 10;
+ x -= 10 * tens;
+ int digits = x;
+
+ writer.write('0' + tens);
+ writer.write('0' + digits);
+ }
+
+ static void write(OutputStream writer, double x) throws Exception{
+ if (x < 0){
+ write(writer, '-');
+ x = -x;
+ }
+
+ x = x + 0.005; // adjust for rounding
+
+ int i = (int)x;
+ write(writer, i);
+ writer.write('.');
+ write(writer, (int)(100 * (x - i)));
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.