stout代码分析之八:cache类
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类的更多相关文章
- stout代码分析之零
最近在使用mesos做高可用设计,在编译的过程中注意到mesos依赖stout,一个仅仅含有头文件的c++基础库.stout代码简洁,设计优雅,值得一读. stout从内容上可细分为以下几块: Pri ...
- stout代码分析之七:Result类
Result类似于Option和Try类的组合,内部有三种状态 enum State { SOME, NONE, ERROR }; SOME表示Result对象有值 NONE表示Result对象值为空 ...
- stout代码分析之五:UUID类
UUID全称通用唯一识别码,被广泛应用于分布式系统中,让所有的元素具有唯一的标识. stout中UUID类继承自boost::uuids::uuid.api如下: random, 产生一个UUID对象 ...
- stout代码分析之一:Duration类
Duration类用于表示时间长度,可精确到纳秒. 代码实现在duration.hpp中,测试代码:duration_tests.cpp 相关api如下: parse, 将字符串转化成Duration ...
- stout代码分析之四:Try类
stout的在异常捕获上遵循于谷歌类似的原则,不适用try...catch...,而是从函数返回值判断异常.Try类正是实现了这样的一个功能. 同Option一样,Try是一个模板类,每个类对象都有两 ...
- stout代码分析之三:Option类
为了安全表示NULL, stout实现了Option类.Option对象有两种状态: enum State { SOME, NONE, }; 其中SOME表示非空,NONE表示为空.可通过isSome ...
- stout代码分析之二:None类
stout库中为了避免使用NULL带来的风险,统一用None表示空. None类的实现方式如下: struct None {}; 奇怪的是, Nothing类实现方式与None一模一样..让人怀疑作者 ...
- stout代码分析之十一:hashmap和multihashmap
hashmap是std::unordered_map的子类,前者对后者的接口做了进一步封装. hashmap的移动构造函数: hashmap(std::map<Key, Value>&am ...
- stout代码分析之十:c++11之move和forward
stout中大量使用了c++11的特性,而c++11中move和forward大概是最神奇的特性了. 左值和右值的区别 ; // a是左值,0是右值 int b = rand(); // b是左值,r ...
随机推荐
- ubuntu server guide 学习笔记
1. 软件包 1.1. dpkg dpkg -l dpkg -l | grep apache2 dpkg -L ufw dpkg -S /etc/host.conf dpkg -i zip_3.0-4 ...
- RNN概述-深度学习 -神经网络
一 RNN概述 前面我们叙述了BP算法, CNN算法, 那么为什么还会有RNN呢?? 什么是RNN, 它到底有什么不同之处? RNN的主要应用领域有哪些呢?这些都是要讨论的问题. 1) BP算法 ...
- Kali信息收集-DNS
1.whois查询 直接在终端输入whois 域名 2.查找dns服务器 (1)host (2)dig (3)nslookup 3.域传输 4.域名枚举 (1)dnsdict6 kali没有集成这款工 ...
- POJ 2986 A Triangle and a Circle(三角形和圆形求交)
Description Given one triangle and one circle in the plane. Your task is to calculate the common are ...
- 关于set和get机制的整理
首先这是es5新增的:定义是设置和获取对象属性时候出发的方法,属于修饰器: 犀牛书例子: function test(n){ return { get count(){ return n }, set ...
- “Hello world!”团队—选题展示
本次选题展示内容: 一.视频展示 链接:http://v.youku.com/v_show/id_XMzA5Mzk5NjYwOA==.html?sharefrom=iphone 视频截图链接:http ...
- Spark Streaming - DStream
1 Overview Spark Streaming is an extension of the core Spark API that enables scalable, high-through ...
- Java中I/O流之缓冲流
Java 中的缓冲流: 1. 缓冲流要“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,提高了读写的效率,同时增加了一些新的方法(带缓冲区的,显著减少对 IO 的读写次数,保护硬盘). 2. ...
- LintCode-67.二叉树的中序遍历
二叉树的中序遍历 给出一棵二叉树,返回其中序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 返回 [1,3,2]. 挑战 你能使用非递归实现么? 标签 递归 二叉树 二叉树遍历 code /** ...
- 图解linux安装tomcat(附常用命令)
本例使用的是centos6.5版本,具体内容如下 一.首先到官方下载tomcat服务 http://tomcat.apache.org/download-70.cgi 二.将tomcat上传至linu ...