diff --git a/.classpath b/.classpath index fb50116..d478271 100644 --- a/.classpath +++ b/.classpath @@ -1,6 +1,7 @@ + diff --git a/README.md b/README.md index 34e1a82..800431b 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,4 @@ JJI3 has made a change to this file. Another change to test the credential helper in git. -EMACSIS THE BEEEEST! \ No newline at end of file +EMACSIS THE BEEEEST! diff --git a/src/Bins.java b/src/Bins.java index b313a3d..24a6d8d 100644 --- a/src/Bins.java +++ b/src/Bins.java @@ -1,10 +1,12 @@ -import java.io.File; -import java.io.FileNotFoundException; + import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.PriorityQueue; import java.util.Scanner; +import java.util.function.Function; +import java.util.stream.Collectors; /** * Runs a number of algorithms that try to fit files onto disks. @@ -26,6 +28,7 @@ public List readData (Scanner input) { return results; } + /** * The main program. */ @@ -33,15 +36,31 @@ public static void main (String args[]) { Bins b = new Bins(); Scanner input = new Scanner(Bins.class.getClassLoader().getResourceAsStream(DATA_FILE)); List data = b.readData(input); + + fitDisksAndPrint(data, l -> l.stream() + .sorted() + .collect(Collectors.toList())); PriorityQueue pq = new PriorityQueue(); pq.add(new Disk(0)); int diskId = 1; - int total = 0; + int total = addItemsToDisk(data, pq, diskId); + reportResultWorstFitMethod(pq, total); + + Collections.sort(data, Collections.reverseOrder()); + pq.add(new Disk(0)); + + diskId = 1; + worstFitDecreasingAddToDisks(data, pq, diskId); + reportResultWorstFitDecreasing(pq); + } + + + private static int worstFitDecreasingAddToDisks(List data, PriorityQueue pq, int diskId) { for (Integer size : data) { Disk d = pq.peek(); - if (d.freeSpace() > size) { + if (d.freeSpace() >= size) { pq.poll(); d.add(size); pq.add(d); @@ -51,25 +70,16 @@ public static void main (String args[]) { d2.add(size); pq.add(d2); } - total += size; - } - - System.out.println("total size = " + total / 1000000.0 + "GB"); - System.out.println(); - System.out.println("worst-fit method"); - System.out.println("number of pq used: " + pq.size()); - while (!pq.isEmpty()) { - System.out.println(pq.poll()); } - System.out.println(); - - Collections.sort(data, Collections.reverseOrder()); - pq.add(new Disk(0)); - - diskId = 1; + return diskId; + } + + private static int addItemsToDisk(List data, PriorityQueue pq, int dID) { + int diskId = dID; + int total = 0; for (Integer size : data) { Disk d = pq.peek(); - if (d.freeSpace() >= size) { + if (d.freeSpace() > size) { pq.poll(); d.add(size); pq.add(d); @@ -79,8 +89,19 @@ public static void main (String args[]) { d2.add(size); pq.add(d2); } + total += size; } + return total; + } + + public static void fitDisksAndPrint(List list, Function, List> func){ + func.apply(list).forEach(i -> System.out.println(i)); + } + + + + private static void reportResultWorstFitDecreasing(PriorityQueue pq) { System.out.println(); System.out.println("worst-fit decreasing method"); System.out.println("number of pq used: " + pq.size()); @@ -89,4 +110,20 @@ public static void main (String args[]) { } System.out.println(); } + + + private static void reportResultWorstFitMethod(PriorityQueue pq, int total) { + System.out.println("total size = " + total / 1000000.0 + "GB"); + System.out.println(); + System.out.println("worst-fit method"); + //System.out.println("worst-fit decreasing method"); + System.out.println("number of pq used: " + pq.size()); + while (!pq.isEmpty()) { + System.out.println(pq.poll()); + } + System.out.println(); + } + + + }