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 ...
随机推荐
- 换抵挡装置 (Kickdown,ACM/ICPC NEERC 2006,UVa1588
题目描述:算法竞赛入门经典习题3-11 题目思路:1.两长条移动匹配 2.上下调换,取小者 #include <stdio.h> #include <string.h> int ...
- 【转】: 探索Lua5.2内部实现:虚拟机指令(3) Upvalues & Globals
在编译期,如果要访问变量a时,会依照以下的顺序决定变量a的类型: a是当前函数的local变量 a是外层函数的local变量,那么a是当前函数的upvalue a是全局变量 local变量本身就存在于 ...
- 【MFC】VS2017新建完MFC后,没有界面,只有代码
问题描述:双击.rc文件后提示在另一个编辑器中打开 解决方法整合: 1----- 打开工程之前先把.rc文件改个名称,然后打开工程双击解决方案管理器的.rc文件, 会显示"载入失败" ...
- C#通过gridview导出excel
[CustomAuthorize] public FileResult ExportQuestionCenterExcel(SearchBaseQuestion search) ...
- [C++] String Basic
Namespace Declarations A using declaration let us use a name from a namespace without qualify the na ...
- c语言中反转字符串的函数strrev(), reverse()
1.使用string.h中的strrev函数 #include<stdio.h> #include<string.h> int main() { char s[]=" ...
- Python-列表练习
1.使用列表生成式生成如下列表:[1,9,25,49,81] s = [i**2 for i in range(1,10)if i%2==1] print(s) 2.输入一个由英文单词组成的字符串(分 ...
- Spring中Controller和RequestMapping的详解
先看一个简单的实例: @Controller @RequestMapping("/hello") public class anyTypeController{ @RequestM ...
- k邻近算法理解及代码实现
github:代码实现 本文算法均使用python3实现 1 KNN KNN(k-nearest neighbor, k近邻法),故名思议,是根据最近的 $ k $ 个邻居来判断未知点属于哪个类别 ...
- jsp连接MYSQL数据库教程(文字+图)
步骤: 1.在mysql官网下载JDBC驱动程序.网址:https://dev.mysql.com/downloads/connector/j/ 2.把里面的jar包(mysql-connector- ...