sugar 自动为DP 加cache (or打表)
// from http://www.csdn.net/article/2015-12-03/2826381 #include <iostream>
#include <tuple>
#include <memory>
#include <map>
#include <functional>
#include <unordered_map> template <class Function>
auto my_sugar(Function func) {
static std::unordered_map<int, int> m; return [&func](int key) { // why not auto key
if (m.find(key) == m.end()) {
m[key] = func(key);
} return m[key]; // overhead is O(1)
};
} //template <typename R, typename... Args>
//std::function<R(Args...)> cache(R(*func)(Args...)) {
// return func;
//} template <typename R, typename... Args>
std::function<R(Args...)> cache(R(*func) (Args...)) {
auto result_map = std::make_shared<std::map<std::tuple<Args...>, R>>(); // key: cache map is in heap return [=](Args... args) {
std::tuple<Args...> t(args...);
std::cout << "result_map size is " << result_map->size() << std::endl;
if (result_map->find(t) == result_map->end()) {
(*result_map)[t] = func(args...);
} return (*result_map)[t];
};
} template <typename R, typename... Args>
std::function<R(Args...)> sugar(R(*func)(Args...), bool needClear = false) {
using function_type = std::function<R(Args...)>;
static std::unordered_map<decltype(func), function_type> functor_map; // pointer => function if (needClear) {
return functor_map[func] = cache(func);
} if (functor_map.find(func) == functor_map.end()) {
functor_map[func] = cache(func);
} return functor_map[func];
} int f(int n) {
return n < ? n : f(n - ) + f(n - );
} int f_my_sugar(int n) {
return n < ? n : my_sugar(f_my_sugar)(n - ) + my_sugar(f_my_sugar)(n - );
} int f_sugar(int n) {
return n < ? n : sugar(f_sugar)(n - ) + sugar(f_sugar)(n - );
} template <class Function>
void test(Function f) {
int start = ;
int n = ;
for (int i = start; i <= n; ++i) {
std::cout << i << ": " << f(i) << std::endl;
}
} int main() {
std::cout << "f ---------------------" << std::endl;
test(f);
std::cout << "f_my_sugar ------------" << std::endl;
test(f_my_sugar);
std::cout << "f_sugar ---------------" << std::endl;
test(f_sugar); return ;
}
sugar 自动为DP 加cache (or打表)的更多相关文章
- 【洛谷4045】[JSOI2009] 密码(状压+AC自动机上DP)
点此看题面 大致题意: 给你\(n\)个字符串,问你有多少个长度为\(L\)的字符串,使得这些字符串都是它的子串.若个数不大于\(42\),按字典序输出所有方案. 状压 显然,由于\(n\)很小,我们 ...
- css居然有根据容器宽度自动截取长度加省略号功能,强大!!
作者:☆威廉古堡♂ 项目中最初的做法(js截取): //字符长度截取包括中英文混合 function subStr(str, len) { str = str.toString(); var newL ...
- bzoj4032/luoguP4112 [HEOI2015]最短不公共子串(后缀自动机+序列自动机上dp)
bzoj4032/luoguP4112 [HEOI2015]最短不公共子串(后缀自动机+序列自动机上dp) bzoj Luogu 题解时间 给两个小写字母串 $ A $ , $ B $ ,请你计算: ...
- EF如何操作内存中的数据以及加载相关联表的数据:延迟加载、贪婪加载、显示加载
之前的EF Code First系列讲了那么多如何配置实体和数据库表的关系,显然配置只是辅助,使用EF操作数据库才是每天开发中都需要用的,这个系列讲讲如何使用EF操作数据库.老版本的EF主要是通过Ob ...
- RX系列四 | RxAndroid | 加载图片 | 提交表单
RX系列四 | RxAndroid | 加载图片 | 提交表单 说实话,学RxJava就是为了我们在Android中运用的更加顺手一点,也就是RxAndroid,我们还是先一步步来,学会怎么去用的比较 ...
- JavaScript之加载表格、表单行数据[插件]
/*** * name:加载表格或表单数据[通用] * name:load-table-or-form-data-common.js * * author:zengtai * date:2017-07 ...
- Hive中将文件加载到数据库表失败解决办法
Hive中将文件加载到数据库表失败解决办法(hive创建表失败) 遇到的问题: FAILED: Execution Error, return code 1 from org.apache.hadoo ...
- HBase 中加盐之后的表如何读取:Spark 篇
在 <HBase 中加盐之后的表如何读取:协处理器篇> 文章中介绍了使用协处理器来查询加盐之后的表,本文将介绍第二种方法来实现相同的功能. 我们知道,HBase 为我们提供了 hbase- ...
- Intellij Idea自动加载改动文件和自动自动热部署加载
1:准备原料 我的Intellij Idea的版本是15. 之后tomcat自动加载修,你只需要在浏览器刷新一下页面即可. ************************************** ...
随机推荐
- unity自定义工具
using UnityEngine;using UnityEditor;using System.Collections;using System.IO;using System.Collection ...
- 多个DLL合并成一个DLL
多个DLL合并成一个DLL,把DLL合并到Exe中的解决方案 1:) 下载 http://download.microsoft.com/download/1/3/4/1347C99E-9DFB-425 ...
- (转)You might not need jQuery
You might not need jQuery You certainly can support IE 9 and below without jQuery, but we don't. Ple ...
- 百度echart使用心得,百度图表。
百度echart算是百度针对数据展示做的一个图表插件吧,一般我们使用都不是问题,主要还是对于对动态数据的解析.我这里使用饼状图,和柱状图为例: 首先,我们需要定义一个绘图的容器:(class是我自己定 ...
- mongoDB学习笔记:了解与安装
初次使用mongoDB是在2013年,项目组为了新产品的研发,使用了mongoDB,作为项目组的一名测试员,也就跟着学起来了. 1.了解mongoDB Mongo DB ,是目前在IT行业非常流行的一 ...
- java 使用递归获取指定文件路径目录,删除指定的文件后缀(可扩展,根据具体需要的删除的后缀进行配置)~~
在工作开发过程中,每次用SVN提交代码全选择的时候,发现会产生很多不需要的文件后缀垃圾文件,感觉挺烦人的,一个一个删太麻烦了,如果产生多种后缀文件时,那删起来多费劲,是吧?想想,就写了一段程序通过递归 ...
- PSP个人(观众界面)
需求:作为一个观众,我希望了解某一场比赛的比分,以便了解赛况.(满意条件:精确到每一局的结果比分) 需求分析:实现查询数据库中每一局的分数并用界面显示. 生成设计文档: 运用三层架构,实现软件的基本功 ...
- backup3
private void changLayoutTemp2(IActiveView activeView, IPageLayout pageLayout, IPageLayout pTempPageL ...
- nf_conntrack被启用导致服务故障
1.查看是否有 nf_conntrack 使用iptable -L -t nat 查看,注意!!!查看也会启用nf_conntrack,任何和nat有关的命令都会启用nf_contrack -L 查 ...
- android开发--多线程
android中的几种多线程实现方式: 1)Activity.runOnUiThread(Runnable) 2)View.post(Runnable) ;View.postDelay(Runnabl ...