stout中实现了LRU cache。cache的成员如下:

public:
typedef std::list<Key> list;
typedef std::tr1::unordered_map<
Key, std::pair<Value, typename list::iterator> > map;

  可以看到map的第二项除了value之外,又有一个指向key的迭代器,这种设计有利于提高cache LRU操作的效率:当查询某个key时,同时获取value和key在list中的迭代器,可方便的将该key和ist中最后一个元素进行交换,如下所示: 

void use(const typename map::iterator& i)
{
// Move the "pointer" to the end of the lru list.
keys.splice(keys.end(), keys, (*i).second.second); // Now update the "pointer" so we can do this again.
(*i).second.second = --keys.end();
}

  这里使用了list.splice交换两个元素的位置。splice的用法如下:

#include <list>
#include <iostream>
#include <algorithm> int main()
{
std::list<int> a = {, , , };
for_each(begin(a), end(a), [](int n){std::cout << n << std::endl;}); a.splice(end(a), a, begin(a));
for_each(begin(a), end(a), [](int n){std::cout << n << std::endl;});
return ;

  关于LRU cache的使用,示例代码如下:

  

#include "stout/cache.hpp"
#include <iostream>
#include <string> int main()
{
Cache<int, std::string> a();
a.put(, "one");
a.put(, "two");
a.put(, "three");
std::cout << a << std::endl; a.get();
std::cout << a << std::endl; a.put(, "four");
std::cout << a << std::endl;
return ;
}

  注:在使用过程中发现cache重载<<操作符的一个编译错误,

template <typename Key, typename Value>
std::ostream& operator << (
std::ostream& stream,
const cache<Key, Value>& c)
{
typename cache<Key, Value>::list::const_iterator i1;
for (i1 = c.keys.begin(); i1 != c.keys.end(); i1++) {
stream << *i1 << ": ";
typename cache<Key, Value>::map::const_iterator i2;
i2 = c.values.find(*i1);
CHECK(i2 != c.values.end());
stream << *i2 << std::endl;
}
return stream;
}

  解决方法:将倒数第三行的

stream << *i2 << std::endl;

  改成

stream << i2->second.first << std::endl;

  即可。

  

stout代码分析之八:cache类的更多相关文章

  1. stout代码分析之零

    最近在使用mesos做高可用设计,在编译的过程中注意到mesos依赖stout,一个仅仅含有头文件的c++基础库.stout代码简洁,设计优雅,值得一读. stout从内容上可细分为以下几块: Pri ...

  2. stout代码分析之七:Result类

    Result类似于Option和Try类的组合,内部有三种状态 enum State { SOME, NONE, ERROR }; SOME表示Result对象有值 NONE表示Result对象值为空 ...

  3. stout代码分析之五:UUID类

    UUID全称通用唯一识别码,被广泛应用于分布式系统中,让所有的元素具有唯一的标识. stout中UUID类继承自boost::uuids::uuid.api如下: random, 产生一个UUID对象 ...

  4. stout代码分析之一:Duration类

    Duration类用于表示时间长度,可精确到纳秒. 代码实现在duration.hpp中,测试代码:duration_tests.cpp 相关api如下: parse, 将字符串转化成Duration ...

  5. stout代码分析之四:Try类

    stout的在异常捕获上遵循于谷歌类似的原则,不适用try...catch...,而是从函数返回值判断异常.Try类正是实现了这样的一个功能. 同Option一样,Try是一个模板类,每个类对象都有两 ...

  6. stout代码分析之三:Option类

    为了安全表示NULL, stout实现了Option类.Option对象有两种状态: enum State { SOME, NONE, }; 其中SOME表示非空,NONE表示为空.可通过isSome ...

  7. stout代码分析之二:None类

    stout库中为了避免使用NULL带来的风险,统一用None表示空. None类的实现方式如下: struct None {}; 奇怪的是, Nothing类实现方式与None一模一样..让人怀疑作者 ...

  8. stout代码分析之十一:hashmap和multihashmap

    hashmap是std::unordered_map的子类,前者对后者的接口做了进一步封装. hashmap的移动构造函数: hashmap(std::map<Key, Value>&am ...

  9. stout代码分析之十:c++11之move和forward

    stout中大量使用了c++11的特性,而c++11中move和forward大概是最神奇的特性了. 左值和右值的区别 ; // a是左值,0是右值 int b = rand(); // b是左值,r ...

随机推荐

  1. Trie 树——搜索关键词提示

    当你在搜索引擎中输入想要搜索的一部分内容时,搜索引擎就会自动弹出下拉框,里面是各种关键词提示,这个功能是怎么实现的呢?其实底层最基本的就是 Trie 树这种数据结构. 1. 什么是 "Tri ...

  2. simhash和minhash实现理解

    文本相似度算法 minhash minhash 1. 把文档A分词形成分词向量L 2. 使用K个hash函数,然后每个hash将L里面的分词分别进行hash,然后得到K个被hash过的集合 3. 分别 ...

  3. MongoDB Sharding 机制分析

    MongoDB Sharding 机制分析 MongoDB 是一种流行的非关系型数据库.作为一种文档型数据库,除了有无 schema 的灵活的数据结构,支持复杂.丰富的查询功能外,MongoDB 还自 ...

  4. POJ 2826 An Easy Problem?!(线段交点+简单计算)

    Description It's raining outside. Farmer Johnson's bull Ben wants some rain to water his flowers. Be ...

  5. ZOJ 3644 Kitty's Game(数论+DP)

    Description Kitty is a little cat. She is crazy about a game recently. There arenscenes in the game( ...

  6. Fafa and the Gates(模拟)

    Two neighboring kingdoms decided to build a wall between them with some gates to enable the citizens ...

  7. Catch That Cow(BFS广搜)

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  8. Coursera-Note: Internet History, Technology and Secure (1st week to 9th week)

    目录 Coursera-Note: Internet History, Technology and Secure 第一周 第二周 数据交换: Packet switching技术: 第三周 创造ht ...

  9. a2

    Alpha 冲刺报告 队名: 组长:吴晓晖 今天完成了哪些任务: 代码量300+,完成了百度地图API的引入. 展示GitHub当日代码/文档签入记录: 明日计划: 整理下这两个功能,然后补些bug ...

  10. ubuntu软件管理apt与dpkg

    目前ubuntu系统主要有dpkg和apt两种软件管理方式两种区别如下 1.dpkg是用来安装.deb文件,但不会解决模块的依赖关系,且不会关心ubuntu的软件仓库内的软件,可以用于安装本地的deb ...