设为首页 加入收藏

TOP

LRU缓存设计(三)
2014-11-24 00:58:12 来源: 作者: 【 】 浏览:11
Tags:LRU 设计

if (_map.size() >= _maxsize) {
Val * old_lru = _lru;
if (_lru->second._newer) {
_lru = _lru->second._newer;
_lru->second._older = NULL;
}
_map.erase(old_lru->first);
}


// insert key to MRU position
std::pair ret
= _map.insert( Val(key, LRUCacheH4Value(V(), _mru, NULL)) );


Val * inserted = &*ret.first;
if (_mru)
_mru->second._newer = inserted;
_mru = inserted;


// possibly update the LRU
if (!_lru)
_lru = _mru;
else if (!_lru->second._newer)
_lru->second._newer = _mru;


return inserted;
}



} // namespace lru


测试代码:


#include


#include "lru.hpp"


using namespace lru;
using namespace std;


int main()
{
typedef LRUCacheH4 CacheType;


CacheType tc(3);


tc.insert(1, 101);
tc.insert(2, 102);
tc.insert(3, 103);

tc.insert(2, 1002);


cout << tc[1] << endl;


cout << "================" << endl;


for (CacheType::const_iterator it = tc.mru_begin(); it != tc.end(); ++it)
cout << it.key() << " " << it.value() << endl;


cout << "================" << endl;


for (CacheType::const_iterator it = tc.lru_begin(); it != tc.end(); ++it)
cout << it.key() << " " << it.value() << endl;


system("PAUSE");
return 0;
}


首页 上一页 1 2 3 下一页 尾页 3/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Linux的信号处理和实际使用(结合R.. 下一篇OpenGL绘制矢量路径的思路

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: