Adjusted list declarations in Best Fit/Worst Fit algorithm code

This commit is contained in:
David Weber 2012-11-24 04:29:34 -05:00
parent 582293d02c
commit 82f9f94598
2 changed files with 80 additions and 60 deletions

View File

@ -9,10 +9,10 @@ import java.util.*;
public class BestFitAlgorithm implements baseAlgorithm{ public class BestFitAlgorithm implements baseAlgorithm{
int memoryBlock[]; private int memoryBlock[];
private Job[] jobArray = new Job[memoryManagement.JOBAMOUNT+10]; private Job[] jobArray = new Job[memoryManagement.JOBAMOUNT+10];
List<Integer> indices; ArrayList<Integer> indices;
List<Integer> blocks; ArrayList<Integer> blocks;
int memoryLocation; int memoryLocation;
int bestSize; //The most suitable block size for the job int bestSize; //The most suitable block size for the job
int bestSizeIndex; //The most suitable block size starting index for the job int bestSizeIndex; //The most suitable block size starting index for the job
@ -21,8 +21,8 @@ public class BestFitAlgorithm implements baseAlgorithm{
{ {
//Initialize memory block to whatever the size is //Initialize memory block to whatever the size is
memoryBlock = new int[memorySize]; memoryBlock = new int[memorySize];
blocks = new ArrayList<>(); //Dynamically resizable array list for allocation candidates (interleaved with index and memory size); blocks = new ArrayList(); //Dynamically resizable array list for allocation candidates (interleaved with index and memory size);
indices = new ArrayList<>(); //Dynamically resizable array list for allocation candidates (interleaved with index and memory size); indices = new ArrayList(); //Dynamically resizable array list for allocation candidates (interleaved with index and memory size);
} }
public int getBestIndex(int jobSize) public int getBestIndex(int jobSize)
@ -31,30 +31,31 @@ public class BestFitAlgorithm implements baseAlgorithm{
indices.clear(); indices.clear();
blocks.clear(); blocks.clear();
synchronized(memoryBlock)
while (memoryLocation < this.memoryBlock.length)
{ {
if (memoryBlock[memoryLocation] != 0){ while (memoryLocation < this.memoryBlock.length)
memoryLocation++;
continue;
}
int beginningLoc = memoryLocation;
int free = 0;
while (memoryLocation < this.memoryBlock.length && memoryBlock[memoryLocation] == 0)
{ {
memoryLocation++; if (memoryBlock[memoryLocation] != 0){
free++; memoryLocation++;
} continue;
}
if (free >= jobSize){ int beginningLoc = memoryLocation;
//System.out.println("Found a block of size " + free + " at " + beginningLoc); int free = 0;
blocks.add(free);
indices.add(beginningLoc); while (memoryLocation < this.memoryBlock.length && memoryBlock[memoryLocation] == 0)
{
memoryLocation++;
free++;
}
if (free >= jobSize){
//System.out.println("Found a block of size " + free + " at " + beginningLoc);
blocks.add(free);
indices.add(beginningLoc);
}
} }
} }
//System.out.println("Size of indices array: " + indices.size()); //System.out.println("Size of indices array: " + indices.size());
//System.out.println("Size of sizes array: " + blocks.size()); //System.out.println("Size of sizes array: " + blocks.size());
@ -148,7 +149,7 @@ public class BestFitAlgorithm implements baseAlgorithm{
*/ */
public void compact() public void compact()
{ {
List<Integer> takenBlocks = new ArrayList<>(); ArrayList<Integer> takenBlocks = new ArrayList();
memoryLocation = 0; memoryLocation = 0;
@ -161,12 +162,19 @@ public class BestFitAlgorithm implements baseAlgorithm{
for(int i = 0; i < takenBlocks.size(); i++) for(int i = 0; i < takenBlocks.size(); i++)
{ {
this.memoryBlock[i] = takenBlocks.get(i).intValue();
synchronized(memoryBlock)
{
this.memoryBlock[i] = takenBlocks.get(i).intValue();
}
} }
for(int i = takenBlocks.size(); i < this.memoryBlock.length; i++) for(int i = takenBlocks.size(); i < this.memoryBlock.length; i++)
{ {
this.memoryBlock[i] = 0; synchronized(memoryBlock)
{
this.memoryBlock[i] = 0;
}
} }
/*System.out.println("Successfully compacted!"); /*System.out.println("Successfully compacted!");
@ -181,9 +189,12 @@ public class BestFitAlgorithm implements baseAlgorithm{
@Override @Override
public void deallocate(int jobSize, int beginningLocation) public void deallocate(int jobSize, int beginningLocation)
{ {
for(int i = beginningLocation; i < jobSize + beginningLocation; i++) synchronized(memoryBlock)
{ {
memoryBlock[i] = 0; for(int i = beginningLocation; i < jobSize + beginningLocation; i++)
{
memoryBlock[i] = 0;
}
} }
} }

View File

@ -11,8 +11,8 @@ public class WorstFitAlgorithm implements baseAlgorithm{
int memoryBlock[]; int memoryBlock[];
private Job[] jobArray = new Job[memoryManagement.JOBAMOUNT+10]; private Job[] jobArray = new Job[memoryManagement.JOBAMOUNT+10];
List<Integer> indices; ArrayList<Integer> indices;
List<Integer> blocks; ArrayList<Integer> blocks;
int memoryLocation; int memoryLocation;
int worstSize; //The most suitable block size for the job int worstSize; //The most suitable block size for the job
int worstSizeIndex; //The most suitable block size starting index for the job int worstSizeIndex; //The most suitable block size starting index for the job
@ -21,8 +21,8 @@ public class WorstFitAlgorithm implements baseAlgorithm{
{ {
//Initialize memory block to whatever the size is //Initialize memory block to whatever the size is
memoryBlock = new int[memorySize]; memoryBlock = new int[memorySize];
blocks = new ArrayList<>(); //Dynamically resizable array list for allocation candidates (interleaved with index and memory size); blocks = new ArrayList(); //Dynamically resizable array list for allocation candidates (interleaved with index and memory size);
indices = new ArrayList<>(); //Dynamically resizable array list for allocation candidates (interleaved with index and memory size); indices = new ArrayList(); //Dynamically resizable array list for allocation candidates (interleaved with index and memory size);
} }
public int getWorstIndex(int jobSize) public int getWorstIndex(int jobSize)
@ -31,27 +31,29 @@ public class WorstFitAlgorithm implements baseAlgorithm{
indices.clear(); indices.clear();
blocks.clear(); blocks.clear();
synchronized(memoryBlock)
while (memoryLocation < this.memoryBlock.length)
{ {
if (memoryBlock[memoryLocation] != 0){ while (memoryLocation < this.memoryBlock.length)
memoryLocation++;
continue;
}
int beginningLoc = memoryLocation;
int free = 0;
while (memoryLocation < this.memoryBlock.length && memoryBlock[memoryLocation] == 0)
{ {
memoryLocation++; if (memoryBlock[memoryLocation] != 0){
free++; memoryLocation++;
} continue;
}
if (free >= jobSize){ int beginningLoc = memoryLocation;
//System.out.println("Found a block of size " + free + " at " + beginningLoc); int free = 0;
blocks.add(free);
indices.add(beginningLoc); while (memoryLocation < this.memoryBlock.length && memoryBlock[memoryLocation] == 0)
{
memoryLocation++;
free++;
}
if (free >= jobSize){
//System.out.println("Found a block of size " + free + " at " + beginningLoc);
blocks.add(free);
indices.add(beginningLoc);
}
} }
} }
@ -148,7 +150,7 @@ public class WorstFitAlgorithm implements baseAlgorithm{
*/ */
public void compact() public void compact()
{ {
List<Integer> takenBlocks = new ArrayList<>(); ArrayList<Integer> takenBlocks = new ArrayList();
memoryLocation = 0; memoryLocation = 0;
@ -158,15 +160,19 @@ public class WorstFitAlgorithm implements baseAlgorithm{
takenBlocks.add(memoryBlock[memoryLocation]); takenBlocks.add(memoryBlock[memoryLocation]);
memoryLocation++; memoryLocation++;
} }
synchronized(memoryBlock)
for(int i = 0; i < takenBlocks.size(); i++)
{ {
this.memoryBlock[i] = takenBlocks.get(i).intValue(); for(int i = 0; i < takenBlocks.size(); i++)
{
this.memoryBlock[i] = takenBlocks.get(i).intValue();
}
} }
synchronized(memoryBlock)
for(int i = takenBlocks.size(); i < this.memoryBlock.length; i++)
{ {
this.memoryBlock[i] = 0; for(int i = takenBlocks.size(); i < this.memoryBlock.length; i++)
{
this.memoryBlock[i] = 0;
}
} }
/* /*
@ -182,9 +188,12 @@ public class WorstFitAlgorithm implements baseAlgorithm{
@Override @Override
public void deallocate(int jobSize, int beginningLocation) public void deallocate(int jobSize, int beginningLocation)
{ {
for(int i = beginningLocation; i < jobSize + beginningLocation; i++) synchronized(memoryBlock)
{ {
memoryBlock[i] = 0; for(int i = beginningLocation; i < jobSize + beginningLocation; i++)
{
memoryBlock[i] = 0;
}
} }
} }