首页 > 其他 > 详细

LRU

时间:2014-02-17 17:20:33      阅读:441      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
import java.util.*;


/*
 * we are using linked list with hashmap for Lru.Reason behind this is ,since HashMap is able to store data and key but
 * how do we get info about least recently used cache value?. For this we need to keep track all inserted data into map
 * by using linked list. When inserting new values , add this value to the top of linked list. When key,value is already
 * present then refresh it by removing it from the linked list and adding it to top of the linked list. 
 * */

public class CacheStrategies {
    
    public interface CacheStrategy<K, T>{
        T get(K key);
        void put(K key,T data);
    }
    
    class CacheStrategyLRU<K, T> implements CacheStrategy<K, T> {
        
        class Node{
            K key;
            T data;
            Node next;
            Node prev;
            
            public Node(K key, T data){
                this.key = key;
                this.data = data;
            }
        }
        
        Node head,tail;
        Map< K, Node> map;
        int maxsize;
        
        public CacheStrategyLRU(int mxsize){
            this.maxsize = mxsize;
            map = new HashMap<K ,Node>();
            head =  new Node(null,null);
            tail = new Node(null,null);
            head.next=tail;
            tail.prev=head;
        }
        
        private void  attach(Node head,Node node){
            node.prev = head;
            node.next = head.next;
            head.next.prev=node;
            head.next = node;
        }
        
        private void remove(Node node){
            node.prev.next = node.next;
            node.next.prev = node.prev;
        }
        
        
        @Override
        public T get(K key) {
            Node node = map.get(key);
            if(node==null){
                return null;
            }
            
            if(map.size()==1){
                return node.data;
            }
            remove(node);
            attach(head,node);
            return node.data;
        }

        @Override
        public void put(K key, T data) {
            if(maxsize<=0){
                return;
            }
            Node node = map.get(key);
            if(node!=null){
                remove(node);
                attach(head,node);
                node.data = data;
            }else{
                if(map.size() >= maxsize){
                    remove(tail.prev);//tail is node pointer ,its not containg any node so delete tail.prev
                    map.remove(tail.prev.key);
                }
                Node nd = new Node(key,data);
                map.put(key, nd);
                attach(head,nd);
            }
            
        }
        
    }  
    
}
bubuko.com,布布扣

LRU

原文:http://www.cnblogs.com/leetcode/p/3552067.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!