From 6621738b2c2718e4287d51d13b285acc17822a53 Mon Sep 17 00:00:00 2001 From: Cambidge Shae Van Wagoner Date: Thu, 3 Mar 2016 16:46:08 -0500 Subject: [PATCH] super magnificent lamba stuff --- .classpath | 6 +++++ src/Bins.java | 69 +++++++++++++++++++++++---------------------------- 2 files changed, 37 insertions(+), 38 deletions(-) create mode 100644 .classpath diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..fb50116 --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/src/Bins.java b/src/Bins.java index b313a3d..73ae3a7 100644 --- a/src/Bins.java +++ b/src/Bins.java @@ -5,6 +5,7 @@ import java.util.List; import java.util.PriorityQueue; import java.util.Scanner; +import java.util.function.Consumer; /** * Runs a number of algorithms that try to fit files onto disks. @@ -25,20 +26,16 @@ public List readData (Scanner input) { } return results; } - - /** - * The main program. - */ - 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); - - PriorityQueue pq = new PriorityQueue(); + + public void fitDisksAndPrint(Scanner input, Consumer> transformList) { + + + List data = readData(input); + transformList.accept(data); + + PriorityQueue pq = new PriorityQueue(); pq.add(new Disk(0)); - int diskId = 1; - int total = 0; for (Integer size : data) { Disk d = pq.peek(); if (d.freeSpace() > size) { @@ -51,10 +48,8 @@ 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()); @@ -62,31 +57,29 @@ public static void main (String args[]) { System.out.println(pq.poll()); } System.out.println(); + } + - Collections.sort(data, Collections.reverseOrder()); - pq.add(new Disk(0)); + /** + * The main program. + */ + public static void main (String args[]) { + Scanner input = new Scanner("data/example.txt"); + Bins b = new Bins(); + b.fitDisksAndPrint(input, (List integers) -> { + int total = 0; + for (int d : integers) { + total += d; } + System.out.println("Total: " + total);}); + input = new Scanner(Bins.class.getClassLoader().getResourceAsStream(DATA_FILE)); + b.fitDisksAndPrint(input, (List integers) -> {Collections.sort(integers); }); + + - diskId = 1; - 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); - } - } + + + - 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(); + } }