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自动加载修,你只需要在浏览器刷新一下页面即可. ************************************** ...
随机推荐
- fw: webdriver 那些坑
http://www.cnblogs.com/huang0925/p/3384596.html 使用WebDriver遇到的那些坑 在做web项目的自动化端到端测试时主要使用的是Selenium ...
- Arch Linux LibreOffice 中文输入法不能切换
From: http://blog.csdn.net/shallowgrave/article/details/8501629 卸载libreoffice-kde4 # pacman -R libre ...
- public static void Invoke (Action action)
using System; using System.Security.Principal; using System.Security.Permissions; namespace Demo { c ...
- 傻瓜式操作Nagios
傻瓜式操作Nagios 不少接触Nagios的朋友都会觉得安装配置困难,应用在企业网中所花费的时间成本很高,下面通过OSSIM来搞定它把. 为了节省资源,首先在淘汰的机器上安装一个低版本的OSSI ...
- python celery + redis
redis http://debugo.com/python-redis celery http://docs.jinkan.org/docs/celery/getting-started/intro ...
- centos 7 相关的一些记录
开80端口: /tcp --permanent 重新加载防火墙: sudo firewall-cmd --reload 安装nginx: sudo rpm -Uvh http://nginx.org/ ...
- java SE编写图形应用程序
借鉴了java 核心技术卷1 并参考http://www.jb51.net/article/56158.htm 添加了JTextField的使用. ####################### ...
- docker help
localhost == 127.0.0.1 == 本机ip ifconfig 或者 ip addr 查看本地宿主机的ip地址 $ docker help Usage: docker [OPTIONS ...
- 深入理解CSS溢出overflow & overflow:hidden真的失效了吗[转载]
深入理解CSS溢出overflow http://www.cnblogs.com/xiaohuochai/p/5289653.html overflow:hidden真的失效了吗 http://www ...
- DELL服务器系统安装
背景环境:DELL poweredge R920 和DELL poweredege R730 新机服务器系统安装 由于以前没有自己单独装过这样的服务器,总感觉复杂,今天实战了几台服务器,挺简单的,为了 ...