一个基于typelist的typemap
前面的typelist的e一个小扩展,http://www.cnblogs.com/flytrace/p/3551414.html.
可以插入pair<key_type, value_type>,然后传入key_type查找对应的value_type.
虽然是map,但仍是线性查找。基本就是一个基于typelist的体力活。
基于二叉树的typemap,可能的思路是通过编译时递增整数常量作为key_type的索引。递归层次复杂了不见得一定会比线性查找来的快。
#ifndef HI_MPL_TYPEMAP_H_INCLUDED
#define HI_MPL_TYPEMAP_H_INCLUDED #include "TypeList.h"
#include <type_traits> template<typename first_, typename second_>
struct pair
{
typedef first_ first;
typedef second_ second;
}; //linear map
//////////////////////////////////////////////////////////
template<typename... TList> struct build_linear_map; template<typename... TList>
struct build_linear_map
{
typedef typelist<TList...> type;
}; template<typename... TList>
struct build_linear_map< typelist<TList...> >
{
typedef typelist<TList...> type;
}; template<typename A, typename... AList, typename B, typename... BList>
struct build_linear_map< typelist<A, AList...>, typelist<B, BList...> >
{
static_assert(sizeof...(AList) == sizeof...(BList), "length is not same");
typedef typename concat< pair<A, B>,
typename build_linear_map< typelist<AList...>, typelist<BList...> >::type >::type type;
}; template<typename A, typename B>
struct build_linear_map< typelist<A>, typelist<B> >
{
typedef typelist< pair<A, B> > type;
}; template<>
struct build_linear_map< typelist<>, typelist<> >
{
typedef typelist<> type;
}; //////////////////////////////////////////////////////////
template<typename Key, typename TList> struct linear_map_find; template<typename Key, typename T, typename... TList>
struct linear_map_find<Key, typelist<T, TList...>>
{
typedef typename linear_map_find<Key, typelist<TList...> >::type type;
}; template<typename Key, typename Second, typename... TList>
struct linear_map_find<Key, typelist< pair<Key, Second>, TList... >>
{
typedef Second type;
}; template<typename Key>
struct linear_map_find<Key, nulllist>
{
typedef nulllist type;
}; //////////////////////////////////////////////////////////
template<typename Key, typename TList> struct linear_map_find_all; template<typename Key, typename T, typename... TList>
struct linear_map_find_all<Key, typelist<T, TList...>>
{
typedef typename linear_map_find_all<Key, typelist<TList...> >::type type;
}; template<typename Key, typename Second, typename... TList>
struct linear_map_find_all<Key, typelist< pair<Key, Second>, TList... > >
{
typedef typename concat<Second, typename linear_map_find_all<Key, typelist<TList...> >::type >::type type;
}; template<typename Key>
struct linear_map_find_all<Key, nulllist>
{
typedef nulllist type;
}; //////////////////////////////////////////////////////////
template<typename T, typename... TList> struct linear_map_add; template<typename T, typename... TList>
struct linear_map_add< T, typelist<TList...> >
{
typedef typename concat<T, typelist<TList...> >::type type;
}; template<typename Key, typename NewValue, typename... TList>
struct linear_map_add<Key, NewValue, typelist<TList...> >
{
typedef typename concat<pair<Key, NewValue>, typelist<TList...> >::type type;
}; //////////////////////////////////////////////////////////
template<typename T, typename... TList> struct linear_map_insert; template<typename T, typename... TList>
struct linear_map_insert<T, typelist<TList...> >
{
static_assert( std::is_same<
typename linear_map_find<typename T::first, typelist<TList...> >::type,
nulllist>::value, "key already exist");
typedef typename concat<T, typelist<TList...> >::type type;
}; template<typename First, typename Second, typename... TList>
struct linear_map_insert<First, Second, typelist<TList...> >
{
static_assert( std::is_same<
typename linear_map_find<First, typelist<TList...> >::type,
nulllist>::value, "key already exist");
typedef typename concat<pair<First, Second>, typelist<TList...> >::type type;
}; //////////////////////////////////////////////////////////
template<typename T, typename... TList> struct linear_map_replace; template<typename T, typename H, typename... TList>
struct linear_map_replace<T, typelist<H, TList...> >
{
typedef typename concat<H, typename linear_map_replace<T, typelist<TList...> >::type>::type type;
}; template<typename Key, typename NewValue, typename OldValue, typename... TList>
struct linear_map_replace<pair<Key, NewValue>, typelist<pair<Key, OldValue>, TList...> >
{
typedef typename concat<pair<Key, NewValue>, typelist<TList...> >::type type;
}; template<typename T>
struct linear_map_replace<T, nulllist>
{
typedef nulllist type;
}; template<typename Key, typename NewValue, typename... TList>
struct linear_map_replace<Key, NewValue, typelist<TList...> >
{
typedef typename linear_map_replace<pair<Key, NewValue>, typelist<TList...> >::type type;
}; //////////////////////////////////////////////////////////
template<typename T, typename... TList> struct linear_map_earse; template<typename T, typename H, typename... TList>
struct linear_map_earse<T, typelist<H, TList...> >
{
typedef typename concat<H, typename linear_map_earse<T, typelist<TList...> >::type>::type type;
}; template<typename Key, typename Value_, typename... TList>
struct linear_map_earse<Key, typelist<pair<Key, Value_>, TList...> >
{
typedef typelist<TList...> type;
}; template<typename T>
struct linear_map_earse<T, nulllist>
{
typedef nulllist type;
}; //////////////////////////////////////////////////////////
template<typename T, typename... TList> struct linear_map_earse_all; template<typename T, typename H, typename... TList>
struct linear_map_earse_all<T, typelist<H, TList...> >
{
typedef typename concat<H, typename linear_map_earse_all<T, typelist<TList...> >::type>::type type;
}; template<typename Key, typename Value_, typename... TList>
struct linear_map_earse_all<Key, typelist<pair<Key, Value_>, TList...> >
{
typedef typename linear_map_earse_all<Key, typelist<TList...>>::type type;
}; template<typename T>
struct linear_map_earse_all<T, nulllist>
{
typedef nulllist type;
}; //////////////////////////////////////////////////////////
template<typename T, typename... TList> struct linear_map_no_duplicate; template<typename T, typename...TList>
struct linear_map_no_duplicate< typelist<T, TList...> >
{
private:
typedef typename linear_map_no_duplicate< typelist<TList...> >::type inner;
typedef typename linear_map_earse<typename T::first, inner>::type inner_result;
public:
typedef typename concat<T, inner_result>::type type;
}; template<>
struct linear_map_no_duplicate< nulllist >
{
typedef nulllist type;
}; #endif // HI_MPL_TYPEMAP_H_INCLUDED
例子:
#include <iostream>
#include "TypeTraits.h"
#include "TypeList.h"
#include "TypeMap.h" int main()
{
typedef build_linear_map<
pair<int, float>,
pair<float, double>,
pair<bool, char>
>::type testmap; typedef typelist<int, float, bool> keylist;
typedef typelist<float, double, char> valuelist;
typedef typelist<float, bool, char> newvaluelist;
typedef build_linear_map<keylist, valuelist>::type testmapcopy;
typedef build_linear_map<keylist, newvaluelist>::type testmapnew; typedef build_linear_map<
pair<int, float>,
pair<float, double>,
pair<bool, char>,
pair<int, char>
>::type testmap2; typedef build_linear_map<
pair<int, float>,
pair<float, double>,
pair<bool, char>,
pair<double, char>
>::type testmap3; typedef build_linear_map<
pair<int, float>,
pair<float, double>,
pair<bool, char>,
pair<double, char>,
pair<double, int>
>::type testmap4; bool b; b = std::is_same<linear_map_find<float, testmap>::type, double>::value;
std::cout << "is same: " << b << std::endl;
b = std::is_same<testmapcopy, testmap>::value;
std::cout << "is same: " << b << std::endl;
b = std::is_same<linear_map_insert<pair<short, long>, testmapcopy>::type,
linear_map_add<pair<short, long>,testmap>::type>::value;
std::cout << "is same: " << b << std::endl;
b = std::is_same<linear_map_replace<pair<float, bool>, testmap>::type, testmapnew>::value;
std::cout << "is same: " << b << std::endl;
b = std::is_same<linear_map_find_all<int, testmap2>::type, typelist<float, char>>::value;
std::cout << "is same: " << b << std::endl;
b = std::is_same<linear_map_earse<double, testmap3>::type, testmap>::value;
std::cout << "is same: " << b << std::endl;
b = std::is_same<linear_map_earse_all<double, testmap4>::type, testmap>::value;
std::cout << "is same: " << b << std::endl;
b = std::is_same<linear_map_no_duplicate<testmap4>::type, testmap3>::value;
std::cout << "is same: " << b << std::endl; return ;
}
一个基于typelist的typemap的更多相关文章
- 一个基于mysql构建的队列表
通常大家都会使用redis作为应用的任务队列表,redis的List结构,在一段进行任务的插入,在另一端进行任务的提取. 任务的插入 $redis->lPush("key:task:l ...
- psutil一个基于python的跨平台系统信息跟踪模块
受益于这个模块的帮助,在这里我推荐一手. https://pythonhosted.org/psutil/#processes psutil是一个基于python的跨平台系统信息监视模块.在pytho ...
- 关于实现一个基于文件持久化的EventStore的核心构思
大家知道enode框架的架构是基于ddd+event sourcing的思想.我们持久化的不是聚合根的最新状态,而是聚合根产生的领域事件.最近我在思考如何实现一个基于文件的eventstore.目标有 ...
- RSuite 一个基于 React.js 的 Web 组件库
RSuite http://rsuite.github.io RSuite 是一个基于 React.js 开发的 Web 组件库,参考 Bootstrap 设计,提供其中常用组件,支持响应式布局. 我 ...
- 【转】发布一个基于NGUI编写的UI框架
发布一个基于NGUI编写的UI框架 1.加载,显示,隐藏,关闭页面,根据标示获得相应界面实例 2.提供界面显示隐藏动画接口 3.单独界面层级,Collider,背景管理 4.根据存储的导航信息完成界面 ...
- CXF 入门:创建一个基于WS-Security标准的安全验证(CXF回调函数使用,)
http://jyao.iteye.com/blog/1346547 注意:以下客户端调用代码中获取服务端ws实例,都是通过CXF 入门: 远程接口调用方式实现 直入正题! 以下是服务端配置 ==== ...
- artDialog是一个基于javascript编写的对话框组件,它拥有精致的界面与友好的接口
artDialog是一个基于javascript编写的对话框组件,它拥有精致的界面与友好的接口 自适应内容 artDialog的特殊UI框架能够适应内容变化,甚至连外部程序动态插入的内容它仍然能自适应 ...
- 一个基于.NET平台的自动化/压力测试系统设计简述
AutoTest系统设计概述 AutoTest是一个基于.NET平台实现的自动化/压力测试的系统,可独立运行于windows平台下,支持分布式部署,不需要其他配置或编译器的支持.(本质是一个基于协议的 ...
- 如何使用 Docker 部署一个基于 Play Framework 的 Scala Web 应用?
本文作者 Jacek Laskowski 拥有近20年的应用程序开发经验,现 CodiLime 的软件开发团队 Leader,曾从 IBM 取得多种资格认证.在这篇博文中,Jacek 分享了 Wars ...
随机推荐
- JAVA文件操作类和文件夹的操作代码示例
JAVA文件操作类和文件夹的操作代码实例,包括读取文本文件内容, 新建目录,多级目录创建,新建文件,有编码方式的文件创建, 删除文件,删除文件夹,删除指定文件夹下所有文件, 复制单个文件,复制整个文件 ...
- 基于Python——实现远程下载sftp文件(只下载.zip文件)
[背景]远程下载发布包等文件时,总是要使用WinSCP等工具登陆拖动.今天就介绍一种使用python下载文件到本地的方法. [代码实现] import paramiko # paramiko模块,基于 ...
- python几种常见的模块安装方法
1. 在线安装 1.1 在命令提示符中运行 pip install package_name 指令 注:具体前置步骤和教程:http://www.cnblogs.com/jfl-xx/p/72895 ...
- 曾经觉得学习晦涩难懂的我是如何爱上linux的
2016年冬天,我已经是一名学习计算机科学与技术专业的大三的“老腊肉”了,但是当时的水平依旧平平.就在2016年快要结束的时候,我周围的同学们被一股考研和工作的压力炸开了锅,我也在默默思考着我的人生, ...
- 1091 N-自守数
如果某个数 K 的平方乘以 N 以后,结果的末尾几位数等于 K,那么就称这个数为“N-自守数”.例如 3×922=25392,而 25392 的末尾两位正好是 92,所以 92 是一个 3-自守 ...
- <? extends T> <? super T>
拿前者来说,这其实就是一个指定的泛型,不过这个泛型可以是T及T的任何子类, 如果一个set方法,是把一个泛型对象T赋值给一个泛型T属性,现在这个T变成了<? extends T>,那么se ...
- Java语法基础学习DayTwelve(泛型)
一.泛型(Generic)在集合中的使用 1.作用 (1)解决元素存储的安全问题 (2)解决获取数据元素时,需要类型强转的问题 2.代码案例 //在集合没有使用泛型的情况下 List list = n ...
- php优秀框架codeigniter学习系列——CI_Output类的学习
这篇文章主要介绍CI核心框架工具类CI_Output. 根据CI文档自己的定义,这个类主要就是生成返回的页面给浏览器.以下选取类中的重点方法进行说明. __construct() 在构造函数中,主要确 ...
- excel图片链接转图片
Sub LoadImage() Dim HLK As Hyperlink, Rng As Range For Each HLK In ActiveSheet.Hyperlinks '循环活动工作表中的 ...
- ssh+key
一.ssh概述 在进行数据传输之前,SSH先对联机数据包通过加密技术进行加密处理,加密后在进行数据传输.确保了传递的数据安全. lrzsz安装包传输本地与Linux SSH客户端(ssh命令)还包含一 ...