一个基于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 ...
随机推荐
- Otto.de:我为什么选择分布式垂直架构
Otto.de:我为什么选择分布式垂直架构 http://cloud.51cto.com/art/201510/493867.htm
- python __init__.py 的作用
__init__.py的主要作用是: . Python中package的标识,不能删除 . 定义__all__用来模糊导入 . 编写Python代码(不建议在__init__中写python模块,可以 ...
- mybatis-generator自动生成代码工具
1.在项目的配置文件中放入配置文件mybatis-generator-config.xml 根据情况修改下配置 <?xml version="1.0" encoding= ...
- jQuery获取包括当前元素的HTML
1.获取当前元素内的HTML (1)方法一 $("#current").html(); (2)方法二 document.getElementById("current&q ...
- 关于NOIP复赛规模的规定
近年来NOIP初赛参赛人数不断增长,复赛规模也相应扩大,但仍不能满足选手积极参赛及扩大普及面的需求,现对NOIP复赛规模的规则调整如下. 1.每个省赛区可以设立多于两个的复赛考点,但必须在同一个城市, ...
- apache 与 php-fpm 几种处理方式
目录 1.SetHandler 2.ProxyPassMatch 3.ProxyPass 1.SetHandler 在apache配置文件只部署一次 需要Apache 2.4.9以上才行 ip:por ...
- NPOI 操作excel之 将图片插入到指定位置;
//新建类 重写Npoi流方法 public class NpoiMemoryStream : MemoryStream { public NpoiMemoryStream() { AllowClos ...
- React项目新手指南
对于程序员而言:驼峰和下划线之间是一场宗派战争:大括号是否换行会成为一种党派:逗号写在行尾还是行首的人来自不同星球…… 然而,无规矩不成方圆,任何一个团队,要想有高质量的产出,第一步必须要对一些基本的 ...
- Linux下命令行cURL的10种常见用法示例
curl的命令行工具功能非常强大,这些数据交互的功能基本上都是通过URL方式进行的,下面这篇文章主要给大家分享了在Linux中命令行cURL的10种常见用法示例,通过示例代码介绍的非常详细,需要的朋友 ...
- 入门项目 A3 src 主代码
import json # 调度内置 json 模块,用于数列化输入输出,相比eval,功能更全面,融合度更高from conf import settings # 从配置文件configure (包 ...