LRU implementation (use list)

The first is the definition of the LRU, LRU represents the least recently used, if the data has recently been visited, then the probability of future access is also higher.

So logic should be new every time the page is accessed into the head of the list, if the list exceeds the length limit, it will list the elements of the tail kick.

 

The main structure, doubly linked list structure of the STL list.

Major operations are get, represents an access key corresponding to the value, this time to inquire doubly linked list, find the key corresponding to the value, and then remove it from the list, insert the head of the list.

     SET, corresponding to the key value represents the set value, this time to find the corresponding key element, it is removed from the list, and then inserted into the head of the list.

Here set up two helpers remove and setHead, are responsible for deleting elements and the elements are added to the list head.

 

Code is implemented as follows:

#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>

using namespace std;

class LRUNode
{
    public:
        int key,value;
        LRUNode(int _key,int _value):key(_key),value(_value)
        {
        }
        bool operator==(LRUNode * p)
        {
            return key==p->key;
        }
};

class LRU
{
    public:
        int get( Int Key);
         void  SET ( int Key, int Val); 
        the LRU ( int _cap): CAP (_cap) 
        { 
        } 
        int CAP; // maximum number of pages stored in the representative 
        void Remove ( int Key);
         void setHead ( int Key , int Val);
         void printLis (); 
        List <LRUNode *> LIS; 
}; 

void the LRU :: printLis () 
{ 
    List <LRUNode *> :: Iterator IT;
     for(it=lis.begin();it!=lis.end();it++)
    {
        cout<<(*it)->key<<" "<<(*it)->value<<endl;
    }
    cout<<endl;
}

void LRU::remove(int key)
{
    LRUNode * searchNode=new LRUNode(key,0);
    list<LRUNode *>::iterator it=find(lis.begin(),lis.end(),searchNode);
    if(it!=lis.end())
    {
        lis.remove(*it);
    }
}

void LRU::setHead(int key,int val)
{
    lis.push_front(new LRUNode(key,val));
}

int LRU::get(int key)
{
    LRUNode * searchNode=new LRUNode(key,0);
    list<LRUNode *>::iterator it=find(lis.begin(),lis.end(),searchNode);
    if(it!=lis.end())
    {
        remove((*it)->key);
        setHead((*it)->key,(*it)->value);
        return (*it)->value;
    }
    return -1;//表示没有找到
}

void LRU::set(int key,int value)
{
    if(lis.size()>=cap)
    {
        lis.pop_back();
    }
    LRUNode * searchNode=new LRUNode(key,0);
    list<LRUNode *>::iterator it=find(lis.begin(),lis.end(),searchNode);
    if(it!=lis.end())
    {
        remove(key);
        setHead(key,value);
    }
    else
    {
        setHead(key,value);
    }
}



int main()
{
    LRU * lru=new LRU(5);
    lru->set(1,1);
    lru->printLis();
    lru->set(2,2);
    lru->printLis();
    lru->set(3,3);
    lru->printLis();
    lru->set(4,4);
    lru->printLis();
    lru->set(5,5);
    lru->printLis();
    lru->set(6,6);
    lru->printLis();
    lru->set(7,7);
    lru->printLis();
    return 0;
}

operation result:

1 1

2 2
1 1

3 3
2 2
1 1

4 4
3 3
2 2
1 1

5 5
4 4
3 3
2 2
1 1

6 6
5 5
4 4
3 3
2 2

7 7
6 6
5 5
4 4
3 3

 

Guess you like

Origin www.cnblogs.com/JsonZhangAA/p/11374439.html