El método de bestSlot es el siguiente:
public Memory bestSlot(@NotNull Process process) {
int i = 0;
int position=0;
boolean allowed = false;
Slot oldSlot = new Slot(size,i);
Slot bestSlot = new Slot(size+1,i);
// if there's no more space we will return false
if(!isSpace(process.getMemory()) || containsProcess(process)) {
return null;
}
// for every Pair of key,value we will search for the optimal slot
for (Map.Entry<Slot,Process> pairEntry: memory.entrySet() ) {
// if we find a slot with the same value as the space we want to divide, and it is empty then
if (pairEntry.getValue()==null){
// if it's the same amount of space we will assign the process to this slot
if (( pairEntry.getKey()).getSpace() == process.getMemory()){
process.setSlot(pairEntry.getKey());
process.setPosition(pairEntry.getKey().getX());
memory.put((pairEntry.getKey()),process);
return this;
}
// else if the space in this slot is bigger then...
else if ((pairEntry.getKey()).getSpace() > process.getMemory()){
// if the auxiliary Slot size is still bigger, then replace for this one
if(bestSlot.getSpace() > (pairEntry.getKey()).getSpace()){
allowed = true;
oldSlot=pairEntry.getKey();
position = (pairEntry.getKey()).getX();
bestSlot.setSpace(process.getMemory());
bestSlot.setX(i);
//memory.remove(((Slot) pairEntry.getKey()));
}
}
}
i++;
}
if (allowed){
pushSlots(position+1);
// we remove the slot that will be replaced by the subtraction of the new space
memory.remove(oldSlot,null);
oldSlot.setSpace(oldSlot.getSpace()-process.getMemory());
oldSlot.setX(position+1);
memory.put(oldSlot,null);
process.setSlot(bestSlot);
memory.put(bestSlot,process);
}
return this;
}
Dicho método analiza los mejores huecos que existen para cada proceso que se pasa por parámetro, haciendo los ajustes necesarios tales como dejar el resto del espacio que está ocupando y si es necesario hacer un push con los elementos del mapa que están por delante