Skip to content
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ Making a change!!!

Even more changes :)


I am changing the readme

138 changes: 85 additions & 53 deletions src/Bins.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;

/**
* Runs a number of algorithms that try to fit files onto disks.
Expand All @@ -31,62 +37,88 @@ public List<Integer> readData (Scanner input) {
*/
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));
b.go();
}
/**
*
*/
public void go(){
Scanner input = new Scanner(Bins.class.getClassLoader().getResourceAsStream(DATA_FILE));
List<Integer> data = this.readData(input);
PriorityQueue<Disk> pq = new PriorityQueue<Disk>();
worstFitDecreasing(pq, data);
worstFit(pq, data);
Integer [] nums = {1, 2, 3, 4, 5, 6, 7, 8};
List<Integer> toTransform = Arrays.asList(nums);
fitDisksAndPrint(toTransform, (in) -> in.stream()
.filter((elem) -> elem%2 == 0).collect(Collectors.toList()));

}

public void fitDisksAndPrint(List<Integer> toTransform, Function<List<Integer>, List<Integer>> func){
List<Integer> transformed = func.apply(toTransform);
transformed.forEach(System.out::println);

}

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;
}
public void worstFitDecreasing(PriorityQueue<Disk> pq, List<Integer> data){
Collections.sort(data, Collections.reverseOrder());
pq.add(new Disk(0));

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();
int 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);
}
}

Collections.sort(data, Collections.reverseOrder());
pq.add(new Disk(0));
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();
}

public void worstFit (PriorityQueue<Disk> pq, List<Integer> data ) {
//algo

pq.add(new Disk(0));

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);
}
}
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();
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");
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();
}

}