c++ LRUCache

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;

template<typename K, typename T>
struct LRUCacheEntry
{
    K key;
    T data;
    LRUCacheEntry* prev;
    LRUCacheEntry* next;
};

template<typename K, typename T>
class LRUCache
{
    private:
        unordered_map<K, LRUCacheEntry<K, T>*> _mapping;
        vector< LRUCacheEntry<K, T>* >         _freeEntries;
        LRUCacheEntry<K, T> *                  head;
        LRUCacheEntry<K, T> *                  tail;
        LRUCacheEntry<K, T> *                  entries;
    public:
        LRUCache(size_t size)
        {
            entries = new LRUCacheEntry<K, T>[size];
            for(int i = 0; i < size; i++ )
            _freeEntries.push_back(entries + i );
            head = new LRUCacheEntry<K, T>;
            tail = new LRUCacheEntry<K, T>;
            head->prev = NULL;
            head->next = tail;
            tail->next = NULL;
            tail->prev = head;
        }

        ~LRUCache()
        {
            delete head;
            delete tail;
            delete [] entries;
        }

        void put(K key, T data)
        {
            LRUCacheEntry<K, T>* node = _mapping[key];
            if(node)
            {
                detach(node);
                node->data = data;
                attach(node);
            }else{
                if(_freeEntries.empty())
                {
                    node = tail->prev;
                    detach(node);
                    _mapping.erase(node->key);
                    node->data = data;
                    node->key = key;
                    _mapping[key] = node;
                    attach(node);
                }else
                {
                    node = _freeEntries.back();
                    _freeEntries.pop_back();
                    node->key = key;
                    node->data = data;
                    _mapping[key] = node;
                    attach(node);
                }
            }
        }

        T get(K key)
        {
            LRUCacheEntry<K, T>* node = _mapping[key];
            if(node)
            {
                detach(node);
                attach(node);
                return node->data;
            }
            else
                return "";
        }

    private:
        void detach(LRUCacheEntry<K, T>* node)
        {
            node->prev->next = node->next;
            node->next->prev = node->prev;
        }
        void attach(LRUCacheEntry<K, T>* node)
        {
            node->next = head->next;
            node->prev = head;
            head->next = node;
            node->next->prev = node;
        }
};

int main(int argc, const char *argv[])
{
  unordered_map<int, int> map_;
  map_[9] = 999;
  cout << map_[9] << endl;
  cout << map_[10] << endl;
  LRUCache<int ,string> lru_cache(100);
  lru_cache.put(1, "one");
  cout << lru_cache.get(1) << endl;
  if(lru_cache.get(2) == "")
      lru_cache.put(2, "two");
  cout << lru_cache.get(2) << endl;
    return 0;
}

c++ LRUCache

时间: 2024-08-28 21:34:00

c++ LRUCache的相关文章

Android内存优化之 LruCache与DiskLruCache

在日常的Adroid开发中我们经常遇到需要处理大量图片的地方,但Android手机的内存有限该怎么避免手机 内存溢出导致app程序oom,google提供了两种解决方式 LruCache LruCache (此类在android-support-v4的包中提供) .这个类非常适合用来缓存图片,它的主要算法原理是把最近使用的对象用强引用存储在 LinkedHashMap 中,并且把最近最少使用的对象在缓存值达到预设定值之前从内存中移除. 在过去,我们经常会使用一种非常流行的内存缓存技术的实现,即软

Android LruCache 压缩图片 有效避免程序OOM

转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/9316683 本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工,英文好的朋友也可以直接去读原文. http://developer.android.com/training/displaying-bitmaps/index.html 压缩加载大图片 我们在编写Android程序的时候经常要用到许多图片,不同图片总是会有不同的形状.不同的大小,但在大多数情况下,这

网络图片的获取以及二级缓存策略(Volley框架+内存LruCache+磁盘DiskLruCache)

在开发安卓应用中避免不了要使用到网络图片,获取网络图片很简单,但是需要付出一定的代价——流量.对于少数的图片而言问题不大,但如果手机应用中包含大量的图片,这势必会耗费用户的一定流量,如果我们不加以处理,每次打开应用都去网络获取图片,那么用户可就不乐意了,这里的处理就是指今天要讲的缓存策略(缓存层分为三层:内存层,磁盘层,网络层). 关于缓存层的工作,当我们第一次打开应用获取图片时,先到网络去下载图片,然后依次存入内存缓存,磁盘缓存,当我们再一次需要用到刚才下载的这张图片时,就不需要再重复的到网络

LruCache源码分析

LRU(Least Recently Used)是一种很常用的资源调度策略,与20/80原则契合,在资源达到上限时倾向保留最近经常访问的资源对象. Android中基于LRU实现了缓存对象,即LruCache,有两处实现:分别为android.support.v4.util.LruCache和android.util.LruCache,其中前者的设计符合Lru, 后者在资源不足时清除的最近经常访问的资源对象,使用时,需要注意这点,此处分析android.support.v4.util.LruCa

面试题:实现LRUCache::Least Recently Used的缩写,意思是最近最少使用,它是一种Cache替换算法

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(

防止多图OOM的核心解决思路就是使用LruCache技术

防止多图OOM的核心解决思路就是使用LruCache技术.但LruCache只是管理了内存中图片的存储与释放,如果图片从内存中被移除的话,那么又需要从网络上重新加载一次图片,这显然非常耗时.对此,Google又提供了一套硬盘缓存的解决方案:DiskLruCache(非Google官方编写,但获得官方认证).只可惜,Android Doc中并没有对DiskLruCache的用法给出详细的说明,而网上关于DiskLruCache的资料也少之又少,因此今天我准备专门写一篇博客来详细讲解DiskLruC

Android提供的LruCache类简介[转载]

转自:here 1 package android.util; 2 3 import java.util.LinkedHashMap; 4 import java.util.Map; 5 6 /** 7 * A cache that holds strong references to a limited number of values. Each time 8 * a value is accessed, it is moved to the head of a queue. When a

使用Lrucache,图片滑动时使用默认图片,停止时加载

注意:LruCache是有版本限制的,低版本的sdk需要在libs文件夹添加相应的support-4v文件.本文改造的大部分是参考http://www.iteye.com/topic/1118828,感谢.不废话直接上工程代码,内有关键注释,项目就不上传了,自己对照着上面网址改呗. 首先是Application文件,负责创建图片存储文件夹: public class MyApp extends Application{ @Override public void onCreate() { sup

LruCache缓存

LruCache通常用于实现内存缓存,采用的缓存算法是LRU(Least Recently Used)即近期最少使用算法,其核心思想是:当缓存满的时候,会优先淘汰那些近期最少使用的缓存对象. 1.LruCache是Android 3.1提供的缓存类,在使用LruCache的时候建议采用support-v4兼容包中提供的LruCache,这样才能兼容Android 2.2版本. 2.LruCache是一个泛型类,内部采用一个LinkedHashMap以强引用的方式存储外界的缓存对象,当缓存满的时候

LruCache源码解析

前言 最近项目要用到Picasso,所以就看了一下Picasso里面的源码,发现里面的内存缓存主要用的LruCache这个类,就去看了一下它的相关的东西,还是挺有收获的. 正文 我一般看类源码喜欢以构造方法作为突破口,然后从它暴露出来的我们使用的最多的那些方法切入,一点一点的把它捋清除,这次基本上也是这个思路. 构造方法 /** * @param maxSize for caches that do not override {@link #sizeOf}, this is * the maxi