Learning notes: implementing OPT FIFO LRU LFU with JAVA

Keywords: Java Apache Back-end

Note: the note is about the implementation of page replacement algorithm by fengsigaoju boss( (35 messages) java implements the page replacement algorithm (OPT,FIFO,LRU) _fengsigaoju's blog - CSDN blog _pagereplacement algorithm code java )Learning, invasion and deletion.

I have been learning JAVA for one month. If there are any deficiencies, please point out.

First code:

import java.io.BufferedInputStream;
import java.util.*;

public class Main {
    private int n;//Inner page storage box
    private int m;//Number of visits
    private int F;//The number of times that cannot be found directly, (F/m) is the page missing rate
    private List<Integer>list=null;//Access address trend
    private Map<Integer,Integer>map=null;
    public Main(){
        F=0;
        map=new HashMap<Integer,Integer>();//Store the contents of each internal storage page box
        Scanner cin=new Scanner(new BufferedInputStream(System.in));
        System.out.println("Please enter the user access page address");
        list=new ArrayList<Integer>();
        String s=cin.nextLine();
        String []s1=s.split(" ");
        m=s1.length;
        for (int i=0;i<m;i++)
            list.add(Integer.valueOf(s1[i]));
        System.out.println("Please enter the number of inner leaf boxes");
        n=cin.nextInt();
        menu();
        switch(cin.nextInt()){
            case 1:OPT();break;
            case 2:FIFO();break;
            case 3:LRU();break;
            case 4:LFU();break;
        }
        cin.close();
    }
    public void menu(){
        System.out.println("**** To select the best replacement algorithm, press 1 **********");
        System.out.println("**** To select a FIFO replacement algorithm, press 2 *******");
        System.out.println("**** To select the nearest and farthest unused replacement algorithm, press 3 ***");
        System.out.println("**** use lfr Press 4 ******************");
    }
    public void OPT(){//Optimal permutation algorithm
        int j;
        for (int i=0;i<m;i++)
        {
            int k=list.get(i);//Pending element
            //System.out.print(map);
            if (!map.containsValue(k)){
                F++;//Cannot directly find times plus 1
                if (map.size()<n){//If it's not full
                    int temp=map.size();
                    map.put(temp, k);
                }
                else{//If it's full
                    int index=0;//Which position will be eliminated
                    int min=0;//Initial maximum length
                    for (int t=0;t<n;t++)
                    {
                        for (j=i+1;j<m;j++){//See which one appears last
                            if (list.get(j)==map.get(t)){//Found for the first time
                                if (j-i>min){
                                    index=t;//Update value
                                    min=j-i;
                                }
                                break;
                            }
                        }
                        if (j==m){//If in the end
                            index=t;
                            min=j-i;
                        }
                    }
                    map.remove(index);
                    map.put(index,k);//Modify table elements
                }
            }
        }
        System.out.println("The bit error rate is:"+F*1.0/m);
    }
    public void FIFO(){//FIFO replacement algorithm
        Queue<Integer>q=new LinkedList<Integer>();
        for (int i=0;i<m;i++)
        {
            int k=list.get(i);//Pending element
            if (!map.containsValue(k)){
                F++;//Cannot directly find times plus 1
                if (map.size()<n){//If it's not full
                    int temp=map.size();
                    map.put(temp, k);
                    q.offer(temp);
                }
                else
                {
                    int temp=q.poll();//Excluded element location
                    map.remove(temp);
                    map.put(temp,k);
                    q.offer(temp);

                }
            }
        }
        System.out.println("The bit error rate is:"+F*1.0/m);
    }
    public void LRU(){//Most recently, no permutation algorithm has been used
        List<Integer>linkedlist=new LinkedList<Integer>();
        int start=0;
        for (int i=0;i<m;i++)
        {
            int k=list.get(i);//Pending element
            if (!map.containsKey(k)){
                F++;//Cannot directly find times plus 1
                if (map.size()<n){//If it's not full
                    int temp=map.size();
                    map.put(k,temp);
                    linkedlist.add(k);//Add location
                }
                else
                {
                    int temp=linkedlist.get(0);
                    int c=map.get(temp);//position
                    map.remove(temp);
                    map.put(k,c);
                    linkedlist.remove(0);
                    linkedlist.add(k);
                }
            }
            else//If you include this value, take it away and press it in later
            {
                int d=linkedlist.indexOf(k);//Find existing location
                linkedlist.remove(d);
                linkedlist.add(k);
            }
        }
        System.out.println("The bit error rate is:"+F*1.0/m);
    }
    public void LFU() {
        for (int i=0;i<m;i++)
        {
            int k=list.get(i);//Pending element
            if (!map.containsValue(k)){
                F++;
                if (map.size()<n){
                    int temp=map.size();
                    map.put(temp, k);
                }
                else{
                    int index=0;
                    int min=0;
                    int num=0;
                    for (int t=0;t<n;t++)
                    {
                        for (int j=i-1;j>0;j--){
                            if (list.get(j)==map.get(t)){
                                num++;
                            }
                            if(min<num){
                                min=num;
                                index=t;
                            }
                        }
                    }
                    map.remove(index);
                    map.put(index,k);
                }
            }
        }
        System.out.println("The bit error rate is:"+F*1.0/m);
    }
    public static  void main(String args[]){
        Main p=new Main();
    }
}

The fourth method I added myself is not particularly good, but it meets the requirements of the textbook.

Next is the understanding of this Code:

1, Code used:

1. The import BufferedInputStream in the first line is a little deeper for beginners. When this line arrives, the original

Scanner cin=new Scanner(new BufferedInputStream(System.in));

Change to

Scanner cin=new Scanner(System.in);

It can also operate normally;

2.List

List <integer> list=new ArrayList<Integer>();

This code allows us to call the built-in functions in the List interface, such as add and get.

3.HashMap

Hash table is a very practical type. Its main feature is that it has key value pairs. From this point alone, it is easy to contact the dictionary in Python, but it has far more functions than it.

Many built-in functions can be judged literally:

containsValue(k) finds whether there is a value of K in the table.

size() returns the number of key value pairs.

put() adds or modifies key value pairs.

remove() moves out the key value pair, and the return value is the deleted value.

get() returns the value corresponding to the key.

4.Queue

q.offer: there are two adding methods in the queue, add and offer. The difference between the two is that add will report an error when the queue is full, and offer will return false, while I haven't found other differences yet.

q.remove: like pop, it deletes the header element. When the queue is empty, remove reports an error and pop returns null.

5.linkedlist

Linked list is a very common type. I won't repeat it here.

2, Problems encountered reading code

1. When reading LRU (), the temp pointer made me dizzy... Later, the compiler found that the temps in the two if are not the same at all... This shows the importance of the compiler

3, Summary

Generally speaking, it is still relatively basic code. School experiments are really difficult. Write notes in this regard to find inspiration. I wanted to write a longer result. My girlfriend asked me to write it for her..

This is the notes of chicken and vegetable that I have studied for a month. If there are mistakes, please correct them.

Posted by ruppelm on Thu, 11 Nov 2021 10:29:03 -0800