Skip to content

Commit

Permalink
Added project files
Browse files Browse the repository at this point in the history
  • Loading branch information
robertcduvall committed Aug 24, 2015
1 parent aa0a192 commit 640c3c0
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 5 deletions.
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="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="data"/>
<classpathentry kind="output" path="bin"/>
</classpath>
24 changes: 24 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Auto detect text files and perform LF normalization
* text=auto

# JAR Files And Dependencies
*.zip binary
*.jar binary
*.dll binary
*.so binary
*.jnilib binary
*.dylib binary

# Standard to msysgit
*.doc diff=astextplain
*.DOC diff=astextplain
*.docx diff=astextplain
*.DOCX diff=astextplain
*.dot diff=astextplain
*.DOT diff=astextplain
*.pdf diff=astextplain
*.PDF diff=astextplain
*.rtf diff=astextplain
*.RTF diff=astextplain

*.obj diff=astextplain
15 changes: 10 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
*.class

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
# Package Files
*.war
*.ear

# Eclipse files
/bin
.project
.metadata/
.settings/

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
5 changes: 5 additions & 0 deletions data/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
700000
200000
800000
100000
150000
20 changes: 20 additions & 0 deletions data/princeton.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
420713
351543
311011
286309
320414
396295
230973
262926
347560
204065
331812
324387
484802
347661
326585
340190
263485
254274
383972
392019
92 changes: 92 additions & 0 deletions src/Bins.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

/**
* Runs a number of algorithms that try to fit files onto disks.
*/
public class Bins {
public static final String DATA_FILE = "example.txt";

/**
* Reads list of integer data from the given input.
*
* @param input tied to an input source that contains space separated numbers
* @return list of the numbers in the order they were read
*/
public List<Integer> readData (Scanner input) {
List<Integer> results = new ArrayList<Integer>();
while (input.hasNext()) {
results.add(input.nextInt());
}
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>();
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");
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;
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();
}
}
106 changes: 106 additions & 0 deletions src/Disk.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import java.util.ArrayList;
import java.util.List;

/**
* Represents a collection of files; how many it can hold is limited by its capacity.
*/
public class Disk implements Comparable<Disk> {
private int myId;
private int mySize;
private int myCapacity;
private List<Integer> myFiles;

/**
* Create an empty Disk.
*/
public Disk () {
mySize = 0;
myCapacity = 1000000;
myFiles = new ArrayList<Integer>();
}

/**
* Create an empty Disk with the given ID.
*/
public Disk (int id) {
myId = id;
mySize = 0;
myCapacity = 1000000;
myFiles = new ArrayList<Integer>();
}

/**
* @return amount of free space available on this disk
*/
public int freeSpace () {
return myCapacity - mySize;
}

/**
* Adds the given file to this disk.
*
* No error checking is done to ensure the file can fit on this disk.
*
* @param filesize size of file to add to this disk
*/
public void add (int filesize) {
myFiles.add(filesize);
mySize += filesize;
}

/**
* Converts this disk's information to a string.
*/
@Override
public String toString () {
String result = myId + "\t" + freeSpace() + ":\t";
for (int k = 0; k < myFiles.size(); k++) {
result += " " + myFiles.get(k);
}
return result;
}

/**
* Check if this disk's information is the same as the given disk's.
*
* Note, currently only equality is based only on their ID.
*
* @param other disk to compare to this disk
* @return true iff their values match
*/
@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;
}
}

/**
* Compare this disk's information to the given disk's for ordering.
*
* Note, currently only equality is based only on their free space.
*
* @param other disk to compare to this disk
* @return positive if this disk is greater than the given disk, zero if they are equal, and
* negative if this disk is less than the given disk
*/
@Override
public int compareTo (Disk other) {
if (other != null) {
int result = other.freeSpace() - freeSpace();
if (result == 0) {
return myId - other.myId;
} else {
return result;
}
} else {
return -1;
}
}
}

0 comments on commit 640c3c0

Please sign in to comment.