Skip to content

Instantly share code, notes, and snippets.

@danielharan
Created April 4, 2010 04:12
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 danielharan/c87c3a3c92889a773d15 to your computer and use it in GitHub Desktop.
Save danielharan/c87c3a3c92889a773d15 to your computer and use it in GitHub Desktop.
diff --git a/ExternalSort.java b/ExternalSort.java
index 6440e59..7d192ba 100644
--- a/ExternalSort.java
+++ b/ExternalSort.java
@@ -75,10 +75,16 @@ public class ExternalSort {
* @param files
* @param output file
*/
- public static int mergeSortedFiles(List<File> files, File outputfile, Comparator<String> cmp) throws IOException {
- PriorityQueue<BinaryFileBuffer> pq = new PriorityQueue<BinaryFileBuffer>();
+ public static int mergeSortedFiles(List<File> files, File outputfile, final Comparator<String> cmp) throws IOException {
+ PriorityQueue<BinaryFileBuffer> pq = new PriorityQueue<BinaryFileBuffer>(11,
+ new Comparator<BinaryFileBuffer>() {
+ public int compare(BinaryFileBuffer i, BinaryFileBuffer j) {
+ return cmp.compare(i.peek(), j.peek());
+ }
+ }
+ );
for (File f : files) {
- BinaryFileBuffer bfb = new BinaryFileBuffer(f,cmp);
+ BinaryFileBuffer bfb = new BinaryFileBuffer(f);
pq.add(bfb);
}
BufferedWriter fbw = new BufferedWriter(new FileWriter(outputfile));
@@ -119,17 +125,15 @@ public class ExternalSort {
}
-class BinaryFileBuffer implements Comparable<BinaryFileBuffer>{
+class BinaryFileBuffer {
public static int BUFFERSIZE = 512;
public BufferedReader fbr;
private List<String> buf = new Vector<String>();
int currentpointer = 0;
- Comparator<String> mCMP;
public File originalfile;
- public BinaryFileBuffer(File f, Comparator<String> cmp) throws IOException {
+ public BinaryFileBuffer(File f) throws IOException {
originalfile = f;
- mCMP = cmp;
fbr = new BufferedReader(new FileReader(f));
reload();
}
@@ -162,10 +166,4 @@ class BinaryFileBuffer implements Comparable<BinaryFileBuffer>{
}
return answer;
}
-
- public int compareTo(BinaryFileBuffer b) {
- return mCMP.compare(peek(), b.peek());
- }
-
-
}
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment