From e98ba2c8d92448f1aa74a64fe6bf777f67d9149c Mon Sep 17 00:00:00 2001 From: Jon Im Date: Thu, 14 Jan 2016 17:41:29 -0500 Subject: [PATCH 1/2] JJI3 SU26 AMB136 --- .classpath | 1 + README.md | 2 +- src/Bins.java | 79 +++++++++++++++++++++++++++++++++------------------ 3 files changed, 53 insertions(+), 29 deletions(-) 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..c285353 100644 --- a/src/Bins.java +++ b/src/Bins.java @@ -25,6 +25,7 @@ public List readData (Scanner input) { } return results; } + /** * The main program. @@ -38,38 +39,66 @@ public static void main (String args[]) { pq.add(new Disk(0)); int diskId = 1; - int total = 0; - for (Integer size : data) { - Disk d = pq.peek(); - if (d.freeSpace() > size) { - pq.poll(); - d.add(size); - pq.add(d); - } else { - Disk d2 = new Disk(diskId); - diskId++; - d2.add(size); - pq.add(d2); - } - total += size; + 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) { + pq.poll(); + d.add(size); + pq.add(d); + } else { + Disk d2 = new Disk(diskId); + diskId++; + d2.add(size); + pq.add(d2); + } + } + return diskId; + } + + + 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()); + while (!pq.isEmpty()) { + System.out.println(pq.poll()); } + System.out.println(); + } - System.out.println("total size = " + total / 1000000.0 + "GB"); + + 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(); + } - Collections.sort(data, Collections.reverseOrder()); - pq.add(new Disk(0)); - diskId = 1; + 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,14 +108,8 @@ public static void main (String args[]) { d2.add(size); pq.add(d2); } + total += size; } - - System.out.println(); - 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(); - } + return total; + } } From 56100795df0caa95603bc7aac5efc06f0b0e90cd Mon Sep 17 00:00:00 2001 From: Jon Im Date: Thu, 3 Mar 2016 16:52:12 -0500 Subject: [PATCH 2/2] Add fitDiskAndPrint --- src/Bins.java | 110 ++++++++++++++++++++++++++++---------------------- 1 file changed, 62 insertions(+), 48 deletions(-) diff --git a/src/Bins.java b/src/Bins.java index c285353..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. @@ -25,7 +27,7 @@ public List readData (Scanner input) { } return results; } - + /** * The main program. @@ -34,54 +36,84 @@ 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 = addItemsToDisk(data, pq, diskId); + 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) { - pq.poll(); - d.add(size); - pq.add(d); - } else { - Disk d2 = new Disk(diskId); - diskId++; - d2.add(size); - pq.add(d2); - } - } - return diskId; - } - - - private static void reportResultWorstFitDecreasing(PriorityQueue pq) { - System.out.println(); + private static int worstFitDecreasingAddToDisks(List data, PriorityQueue pq, int diskId) { + for (Integer size : data) { + Disk d = pq.peek(); + if (d.freeSpace() >= size) { + pq.poll(); + d.add(size); + pq.add(d); + } else { + Disk d2 = new Disk(diskId); + diskId++; + d2.add(size); + pq.add(d2); + } + } + 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) { + pq.poll(); + d.add(size); + pq.add(d); + } else { + Disk d2 = new Disk(diskId); + diskId++; + 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()); while (!pq.isEmpty()) { System.out.println(pq.poll()); } System.out.println(); - } + } - private static void reportResultWorstFitMethod(PriorityQueue pq, int total) { - System.out.println("total size = " + total / 1000000.0 + "GB"); + 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"); @@ -90,26 +122,8 @@ private static void reportResultWorstFitMethod(PriorityQueue pq, int total System.out.println(pq.poll()); } System.out.println(); - } + } - 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) { - pq.poll(); - d.add(size); - pq.add(d); - } else { - Disk d2 = new Disk(diskId); - diskId++; - d2.add(size); - pq.add(d2); - } - total += size; - } - return total; - } + }