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

Dmm68 jji3 #49

Open
wants to merge 2 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
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -1,6 +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>
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ JJI3 has made a change to this file.

Another change to test the credential helper in git.

EMACSIS THE BEEEEST!
EMACSIS THE BEEEEST!
77 changes: 57 additions & 20 deletions src/Bins.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -26,22 +28,39 @@ 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);

fitDisksAndPrint(data, l -> l.stream()
.sorted()
.collect(Collectors.toList()));

PriorityQueue<Disk> pq = new PriorityQueue<Disk>();
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<Integer> data, PriorityQueue<Disk> 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);
Expand All @@ -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<Integer> data, PriorityQueue<Disk> 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);
Expand All @@ -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<Integer> list, Function<List<Integer>, List<Integer>> func){
func.apply(list).forEach(i -> System.out.println(i));
}




private static void reportResultWorstFitDecreasing(PriorityQueue<Disk> pq) {
System.out.println();
System.out.println("worst-fit decreasing method");
System.out.println("number of pq used: " + pq.size());
Expand All @@ -89,4 +110,20 @@ public static void main (String args[]) {
}
System.out.println();
}


private static void reportResultWorstFitMethod(PriorityQueue<Disk> 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();
}



}