Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

csv5, reb36 #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
69 changes: 31 additions & 38 deletions src/Bins.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -25,20 +26,16 @@ public List<Integer> 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<Integer> data = b.readData(input);

PriorityQueue<Disk> pq = new PriorityQueue<Disk>();

public void fitDisksAndPrint(Scanner input, Consumer<List<Integer>> transformList) {


List<Integer> data = readData(input);
transformList.accept(data);

PriorityQueue<Disk> pq = new PriorityQueue<Disk>();
pq.add(new Disk(0));

int diskId = 1;
int total = 0;
for (Integer size : data) {
Disk d = pq.peek();
if (d.freeSpace() > size) {
Expand All @@ -51,42 +48,38 @@ 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));
/**
* The main program.
*/
public static void main (String args[]) {
Scanner input = new Scanner("data/example.txt");
Bins b = new Bins();
b.fitDisksAndPrint(input, (List<Integer> 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<Integer> 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();

}
}