Implementation of LRU algorithm with static variable array

Source: Internet
Author: User

For an explanation of the LRU algorithm, see https://baike.baidu.com/item/LRU/1269842

Baidu Encyclopedia here gives a more detailed, and then there is an example to say

LRU (least recently used) is least recently used. Assuming that the sequence is 4 3 4 2 3 1 4 2 physical blocks have 3 then the first round 4 into memory 4 times turn 3 into memory 3 4 after 4 dial memory 4 3 after 2 dial in memory 2 4 3 after 3 into memory 3 2 4 after 1 into memory 1 3 2 (because the minimum used is 4, so drop 4 ) then 4 into memory 4 1 3 (same principle) last 2 into memory 2 4 1 also such as: Consider the following page trend: 1,2,3,4,2,1,5,6,2,1,2,3,7,6,3,2,1,2,3,61 12 2 13 3 2 14 4 3 2 12 2 4 3 11 1 2 4 35 5 1 2 46 6 5 1 22 2 6 5 11 1 2 6 52 2 1 6 53 3 2 1 67 7 3 2 16 6 7 3 23 3 6 7 22 2 3 6 71 1 2 3 62 2 1 3 63 3 2 1 66 6 3 2 1 So how do you want to achieve this effect with PHP? I give the core function as follows
<?PHPfunctionLru$into _data=""){    Static $array=Array(); $max _length= 5;//Maximum length    if(Empty($array))    {        $array[]=$into _data; }Else    {        //The description is not empty and is not empty to find        $find _index=Array_search($into _data,$array); if($find _index!==false)        {            //If you find what you found, put it to the first one .            unset($array[$find _index]);//get rid of this and take the first one.            Array_unshift($array,$into _data);//put it on the first one.}Else        {            //not finding out whether the maximum length is reached if the last one is removed            if(Count($array)==$max _length-1)            {                //reach Maximum length//Remove Last                Array_pop($array); Array_unshift($array,$into _data);//put it on the first one.}Else            {                Array_unshift($array,$into _data);//put it on the first one.            }        }        $array=array_values($array);//Array Reset    }    return $array;}?>

The calling code is as follows:

<? PHP $array=array(1,2,3,4,2,1,5,6,2,1,2,3,7,6,3,2,1,2,3,6); foreach ($array as$key$v) {    $now=lru ($v);     Echo ($v). " ". (implode("",$now)). " <br/> ";}? >

The effect is as follows:

The results are as follows:

That's what we want!

Implementation of LRU algorithm with static variable array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.