Changeset 862 for cs420

Show
Ignore:
Timestamp:
09/27/07 12:58:18 (2 years ago)
Author:
vasi
Message:

Merging should work

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • cs420/asst3/asst3420.java

    r861 r862  
    1414        private static final int compareBlocks(byte[] a, byte[] b) { 
    1515                // We'll just assume a lexical comparison 
    16                 return a.compareTo(b); 
     16                for (int i = 0; i < BLOCKSIZE; ++i) { 
     17                        int d = a[i] - b[i]; 
     18                        if (d != 0) 
     19                                return d; 
     20                } 
     21                return 0; 
    1722        } 
    1823         
     
    3237        } 
    3338         
     39        private static void copy(DataInputStream in, DataOutputStream out, 
     40                        byte[] buf) throws IOException { 
     41                try { 
     42                        while (true) { 
     43                                in.readFully(buf); 
     44                                out.write(buf); 
     45                        } 
     46                } catch (EOFException e) { 
     47                        // done! 
     48                } finally { 
     49                        in.close(); 
     50                } 
     51        } 
     52         
    3453        private static class Statistics { 
    3554                public int onlyFirst = 0, onlySecond = 0, both = 0; 
    3655        } 
    3756         
    38         private static String readBlock(DataInput in, byte[] buf) { 
    39                 return  
    40         } 
    41          
    4257        // Do a two way merge 
    4358        private static Statistics merge2(InputStream is1, InputStream is2, 
    44                         OutputStream os) { 
     59                        OutputStream os) throws IOException { 
    4560                Statistics stats = new Statistics(); 
    46                 DataInput in1 = new DataInputStream(is1), 
     61                DataInputStream in1 = new DataInputStream(is1), 
    4762                        in2 = new DataInputStream(is2); 
    48                 DataOutput out = new DataOutputStream(os); 
     63                DataOutputStream out = new DataOutputStream(os); 
    4964                 
     65                byte[] b1 = new byte[BLOCKSIZE], b2 = new byte[BLOCKSIZE]; 
     66                boolean v1 = false, v2 = false; // Do b1 and b2 have valid, cur data? 
    5067                 
    51                 String d1 = null, d2 = null; 
    52                  
     68                try { 
     69                        while (true) { 
     70                                // Do any reading necessary 
     71                                if (!v1) { 
     72                                        in1.readFully(b1); 
     73                                        v1 = true; 
     74                                } 
     75                                if (!v2) { 
     76                                        in1.readFully(b2); 
     77                                        v2 = true; 
     78                                } 
     79                                 
     80                                int c = compareBlocks(b1, b2); 
     81                                if (c == 0) { 
     82                                        out.write(b1); 
     83                                        // Assume streams are already filtered for dupes! 
     84                                        v1 = v2 = false; 
     85                                        ++stats.both; 
     86                                } else if (c < 0) { 
     87                                        out.write(b1); 
     88                                        v1 = false; 
     89                                        ++stats.onlyFirst; 
     90                                } else { 
     91                                        out.write(b2); 
     92                                        v2 = false; 
     93                                        ++stats.onlySecond; 
     94                                } 
     95                        } 
     96                } catch (EOFException e) { 
     97                        // Just output whatever's left 
     98                        if (v1) 
     99                                out.write(b1); 
     100                        if (v2) 
     101                                out.write(b2);                   
     102                        copy(in1, out, b1); 
     103                        copy(in2, out, b2); 
     104                } finally { 
     105                        out.close(); 
     106                } 
    53107                 
    54108                return stats;