123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package cache;
- /**
- *
- * @author Juninho Carlos
- */
- public class LRU {
- private int[] traceLRU;
-
- public LRU(){
- this.traceLRU = new int[4];
- for(int i = 0; i < 4; i++){
- this.traceLRU[i] = -1;
- }
- }
-
- public int[] getTraceLRU(){
- return this.traceLRU;
- }
-
- private boolean isCheio(){
- for(int i = 0; i < 4; i++){
- if(this.traceLRU[i]==-1){
- return false;
- }
- }
- return true;
- }
-
- private int getIndexFromBloco(int num_bloco){
- for(int i = 0; i < 4; i++){
- if(this.traceLRU[i] == num_bloco){
- return i;
- }
- }
- return -1;
- }
-
- private boolean containsBloco(int num_bloco){
- for(int i = 0; i < 4; i++){
- if(this.traceLRU[i] == num_bloco){
- return true;
- }
- }
- return false;
- }
-
- private void atualizaTraceLRU(int indexDoAcesso){
- int bloco = this.traceLRU[indexDoAcesso];
-
- for(; indexDoAcesso < 3 && this.traceLRU[indexDoAcesso] != -1 ;indexDoAcesso++){
- this.traceLRU[indexDoAcesso] = this.traceLRU[indexDoAcesso+1];
- }
- this.traceLRU[indexDoAcesso-1] = bloco;
- }
-
- private void insereNoFinal(int numBloco){
- for(int i =0; i<4; i++){
- if(this.traceLRU[i] == -1){
- this.traceLRU[i] = numBloco;
- return;
- }
- }
- }
-
- public void atualizaLRU(int bloco){
- //Caso o trace ainda não tenha sido preenchido
-
-
- if(!isCheio() && containsBloco(bloco)){
-
- this.atualizaTraceLRU(getIndexFromBloco(bloco));
- return;
- }else if(!isCheio()){
-
- this.insereNoFinal(bloco);
- return;
- }
-
-
- //Caso já tenha sido preenchido
- int indiceBloco = 0;
- for(; indiceBloco < 4; indiceBloco++){
- if(this.traceLRU[indiceBloco] == bloco){
- break;
- }
- }
-
- for(; indiceBloco < 3;indiceBloco++){
- this.traceLRU[indiceBloco] = this.traceLRU[indiceBloco+1];
- }
-
- this.traceLRU[3] = bloco;
- }
-
- public int getBlocoFromLru(){
- return this.traceLRU[0];
- }
- @Override
- public String toString() {
- String s = "[";
- for(int i = 0; i < 4;i++){
- s += this.traceLRU[i] + ",";
- }
- s += "]\n";
- return s; //To change body of generated methods, choose Tools | Templates.
- }
-
-
-
- }
|