前面的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的更多相关文章

  1. 一个基于mysql构建的队列表

    通常大家都会使用redis作为应用的任务队列表,redis的List结构,在一段进行任务的插入,在另一端进行任务的提取. 任务的插入 $redis->lPush("key:task:l ...

  2. psutil一个基于python的跨平台系统信息跟踪模块

    受益于这个模块的帮助,在这里我推荐一手. https://pythonhosted.org/psutil/#processes psutil是一个基于python的跨平台系统信息监视模块.在pytho ...

  3. 关于实现一个基于文件持久化的EventStore的核心构思

    大家知道enode框架的架构是基于ddd+event sourcing的思想.我们持久化的不是聚合根的最新状态,而是聚合根产生的领域事件.最近我在思考如何实现一个基于文件的eventstore.目标有 ...

  4. RSuite 一个基于 React.js 的 Web 组件库

    RSuite http://rsuite.github.io RSuite 是一个基于 React.js 开发的 Web 组件库,参考 Bootstrap 设计,提供其中常用组件,支持响应式布局. 我 ...

  5. 【转】发布一个基于NGUI编写的UI框架

    发布一个基于NGUI编写的UI框架 1.加载,显示,隐藏,关闭页面,根据标示获得相应界面实例 2.提供界面显示隐藏动画接口 3.单独界面层级,Collider,背景管理 4.根据存储的导航信息完成界面 ...

  6. CXF 入门:创建一个基于WS-Security标准的安全验证(CXF回调函数使用,)

    http://jyao.iteye.com/blog/1346547 注意:以下客户端调用代码中获取服务端ws实例,都是通过CXF 入门: 远程接口调用方式实现 直入正题! 以下是服务端配置 ==== ...

  7. artDialog是一个基于javascript编写的对话框组件,它拥有精致的界面与友好的接口

    artDialog是一个基于javascript编写的对话框组件,它拥有精致的界面与友好的接口 自适应内容 artDialog的特殊UI框架能够适应内容变化,甚至连外部程序动态插入的内容它仍然能自适应 ...

  8. 一个基于.NET平台的自动化/压力测试系统设计简述

    AutoTest系统设计概述 AutoTest是一个基于.NET平台实现的自动化/压力测试的系统,可独立运行于windows平台下,支持分布式部署,不需要其他配置或编译器的支持.(本质是一个基于协议的 ...

  9. 如何使用 Docker 部署一个基于 Play Framework 的 Scala Web 应用?

    本文作者 Jacek Laskowski 拥有近20年的应用程序开发经验,现 CodiLime 的软件开发团队 Leader,曾从 IBM 取得多种资格认证.在这篇博文中,Jacek 分享了 Wars ...

随机推荐

  1. day037 mysql之单表查询

    一.单表查询语法 select distinct 字段1,字段2,... from 库名.表名 where 条件 group by 字段 having 筛选条件 order by 字段 limit 限 ...

  2. 全排列 next_permutation 用法

    给一个正整数n,让你求它的全排列 先介绍一个函数,iota(a,a+n,1) 用法就是把a数组的第0位到第n-1位依次赋为1,2,.....n: 然后是next_permutation(a,a+4)函 ...

  3. MongoDB AUTH结果验证及开启方法

          创建超级管理员(root)和普通用户(gxpt) #创建超级管理员(root) RS1:PRIMARY> use admin RS1:PRIMARY> db.createUse ...

  4. SAP WDA 自建Portal

    1.新建Package:ZWDA. 2.新建Portal用户表:ZPORTAL_USER 上图中是基于CRM的BP来做的用户表,如果针对其他系统可以使用组织架构里的角色用户... 3.新建用户视图配置 ...

  5. 基于c#的windows基础设计(学习日记2)【关于多态】

    这次的多态很简单,没什么知识点 . 直接贴代码了: public abstract class Animal //建立一个抽象类 { private bool m_sex; private strin ...

  6. CST时区,MYSQL与JAVA-WEB服务器时间相差13个小时的问题

    最近倒腾了一台阿里云主机,打算在上面装点自己的应用.使用docker安装了安装mysql后,发现数据库的存储的时间与java-web应用的时间差8个小时,初步怀疑是docker容器时区的问题.经过一系 ...

  7. firewall-cmd.man

    FIREWALL-CMD(1) firewall-cmd FIREWALL-CMD(1) NAME firewall-cmd - firewalld command line client SYNOP ...

  8. Linux3.10.0块IO子系统流程(5)-- 为SCSI命令准备聚散列表

    SCSI数据缓冲区组织成聚散列表的形式.Linux内核中表示聚散列表的基本数据结构是scatterlist,虽然名字中有list,但它只对应一个内存缓冲区,聚散列表就是多个scatterlist的组合 ...

  9. oracle数据库SQL入门

    1.oracle数据库的下载.安装.卸载 Oracle下载: Oracle下载链接地址:http://www.oracle.com/technetwork/cn/database/enterprise ...

  10. 一、JAVA变量类型:①类变量与实例变量的异同点

    在JAVA中,变量使用前必须声明,格式如下: int a; //单个变量声明 int b, c, d; //多个变量一起声明 int e = 1, f = 2, g = 3; //声明时同时赋值(初始 ...