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,7 +31,8 @@ public class BestFitAlgorithm implements baseAlgorithm{
indices.clear(); indices.clear();
blocks.clear(); blocks.clear();
synchronized(memoryBlock)
{
while (memoryLocation < this.memoryBlock.length) while (memoryLocation < this.memoryBlock.length)
{ {
if (memoryBlock[memoryLocation] != 0){ if (memoryBlock[memoryLocation] != 0){
@ -54,7 +55,7 @@ public class BestFitAlgorithm implements baseAlgorithm{
indices.add(beginningLoc); 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;
@ -160,14 +161,21 @@ public class BestFitAlgorithm implements baseAlgorithm{
} }
for(int i = 0; i < takenBlocks.size(); i++) for(int i = 0; i < takenBlocks.size(); i++)
{
synchronized(memoryBlock)
{ {
this.memoryBlock[i] = takenBlocks.get(i).intValue(); 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++)
{
synchronized(memoryBlock)
{ {
this.memoryBlock[i] = 0; this.memoryBlock[i] = 0;
} }
}
/*System.out.println("Successfully compacted!"); /*System.out.println("Successfully compacted!");
@ -180,11 +188,14 @@ public class BestFitAlgorithm implements baseAlgorithm{
@Override @Override
public void deallocate(int jobSize, int beginningLocation) public void deallocate(int jobSize, int beginningLocation)
{
synchronized(memoryBlock)
{ {
for(int i = beginningLocation; i < jobSize + beginningLocation; i++) for(int i = beginningLocation; i < jobSize + beginningLocation; i++)
{ {
memoryBlock[i] = 0; 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,7 +31,8 @@ public class WorstFitAlgorithm implements baseAlgorithm{
indices.clear(); indices.clear();
blocks.clear(); blocks.clear();
synchronized(memoryBlock)
{
while (memoryLocation < this.memoryBlock.length) while (memoryLocation < this.memoryBlock.length)
{ {
if (memoryBlock[memoryLocation] != 0){ if (memoryBlock[memoryLocation] != 0){
@ -54,6 +55,7 @@ public class WorstFitAlgorithm implements baseAlgorithm{
indices.add(beginningLoc); 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 +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,16 +160,20 @@ 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++) for(int i = 0; i < takenBlocks.size(); i++)
{ {
this.memoryBlock[i] = takenBlocks.get(i).intValue(); this.memoryBlock[i] = takenBlocks.get(i).intValue();
} }
}
synchronized(memoryBlock)
{
for(int i = takenBlocks.size(); i < this.memoryBlock.length; i++) for(int i = takenBlocks.size(); i < this.memoryBlock.length; i++)
{ {
this.memoryBlock[i] = 0; this.memoryBlock[i] = 0;
} }
}
/* /*
System.out.println("Successfully compacted!"); System.out.println("Successfully compacted!");
@ -181,11 +187,14 @@ public class WorstFitAlgorithm implements baseAlgorithm{
@Override @Override
public void deallocate(int jobSize, int beginningLocation) public void deallocate(int jobSize, int beginningLocation)
{
synchronized(memoryBlock)
{ {
for(int i = beginningLocation; i < jobSize + beginningLocation; i++) for(int i = beginningLocation; i < jobSize + beginningLocation; i++)
{ {
memoryBlock[i] = 0; memoryBlock[i] = 0;
} }
} }
}
} }