LRUああああああ

<?php
error_reporting(0);
class LRUCache {
private $capacity;
private $list;
/**
* @param Integer $capacity
*/
function __construct($capacity) {
$this->capacity=$capacity;
$this->list=new HashList();

}

/**
* @param Integer $key
* @return Integer
*/
function get($key) {
if($key<0) return -1;
return $this->list->get($key);
}

/**
* @param Integer $key
* @param Integer $value
* @return NULL
*/
function put($key, $value) {
$size=$this->list->size;
$isHas=$this->list->checkIndex($key);
if($isHas || $size+1 > $this->capacity){
$this->list->removeNode($key);
}
$this->list->addAsHead($key,$value);
}
}

class HashList{
public $head;
public $tail;
public $size;
public $buckets=[];
public function __construct(Node $head=null,Node $tail=null){
$this->head=$head;
$this->tail=$tail;
$this->size=0;
}

//检查键是否存在
public function checkIndex($key){
$res=$this->buckets[$key];
if($res){
return true;
}
return false;
}

public function get($key){
$res=$this->buckets[$key];
if(!$res) return -1;
$this->moveToHead($res);
return $res->val;
}

//新加入的节点
public function addAsHead($key,$val)
{
$node=new Node($val);
if($this->tail==null && $this->head !=null){
$this->tail=$this->head;
$this->tail->next=null;
$this->tail->pre=$node;
}
$node->pre=null;
$node->next=$this->head;
$this->head->pre=$node;
$this->head=$node;
$node->key=$key;
$this->buckets[$key]=$node;
$this->size++;
}

//移除指针(已存在的键值对或者删除最近最少使用原则)
public function removeNode($key)
{
$current=$this->head;
for($i=1;$i<$this->size;$i++){
if($current->key==$key) break;
$current=$current->next;
}
unset($this->buckets[$current->key]);
//调整指针
if($current->pre==null){
$current->next->pre=null;
$this->head=$current->next;
}else if($current->next ==null){
$current->pre->next=null;
$current=$current->pre;
$this->tail=$current;
}else{
$current->pre->next=$current->next;
$current->next->pre=$current->pre;
$current=null;
}
$this->size--;

}

//把对应的节点应到链表头部(最近get或者刚刚put进去的node节点)
public function moveToHead(Node $node)
{
if($node==$this->head) return ;
//调整前后指针指向
$node->pre->next=$node->next;
$node->next->pre=$node->pre;
$node->next=$this->head;
$this->head->pre=$node;
$this->head=$node;
$node->pre=null;
}

}

class Node{
public $key;
public $val;
public $next;
public $pre;
public function __construct($val)
{
$this->val=$val;
}
}



 $obj =  LRUCache(3);
$obj->put(1, '2');
 $ret_1 = $obj->get(1);
 var_dump($ret_1);die;


おすすめ

転載: blog.csdn.net/qq_40192867/article/details/123214485