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

nsl8 and ras70 #42

Open
wants to merge 3 commits 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
7 changes: 7 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="data"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
90 changes: 49 additions & 41 deletions src/Bins.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
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.
Expand All @@ -25,48 +27,30 @@ public List<Integer> readData (Scanner input) {
}
return results;
}

/**
* The main program.
* Method to write all output messages
*/
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>();
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;
}

System.out.println("total size = " + total / 1000000.0 + "GB");
public static void output (PriorityQueue<Disk> pq, String heuristic, int total ){
System.out.println("total size = " + total / 1000000.0 + "GB");
System.out.println();
System.out.println("worst-fit method");
System.out.println(heuristic + " 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;
}

/**
* Method that will iterate through the files and places them into disks, returning
* the disks required
*/

public static int place (PriorityQueue<Disk> pq, List<Integer> data){
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 @@ -79,14 +63,38 @@ public static void main (String args[]) {
d2.add(size);
pq.add(d2);
}
total += size;
}

return total;
}

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();
/**
* 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>();

output(pq, "worst-fit", place(pq, data));

Collections.sort(data, Collections.reverseOrder());
output(pq, "worst-fit decreasing", place(pq, data));
}


public void fitDisksAndPrint(List<Integer> list){
transformAndPrint(list, (input) ->
input.stream()
.sorted()
.collect(Collectors.toList()));
}

private void transformAndPrint (List<Integer> list, Function<List<Integer>, List<Integer>> func) {
List<Integer> transformed = func.apply(list);
transformed.forEach(System.out::println);

}
}
16 changes: 6 additions & 10 deletions src/Disk.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,10 @@ public String toString () {
*/
@Override
public boolean equals (Object other) {
if (other != null && other instanceof Disk) {
if (myId == ((Disk) other).myId) {
return true;
} else {
return false;
}
} else {
return false;
}
if (other != null && other instanceof Disk ) {
return myId == ((Disk) other).myId;
}
return false;
}

/**
Expand All @@ -99,7 +94,8 @@ public int compareTo (Disk other) {
} else {
return result;
}
} else {
}
else {
return -1;
}
}
Expand Down