前面的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. linux存储管理之磁盘配额

    磁盘配额 1  相关命令:quota.quotacheck.edquota.quotaon.quotaoffquota要使用的命令有两种:一种用于查询功能,包括quota.quotacheck.quo ...

  2. css及HTML知识点

    html : 180°  输出为 css:    margin: 0 auto;会在页面水平居中显示  box-shadow: 0 0 5px #f61818; 设置投影的位置大小颜色 outline ...

  3. recon工具解读

    recon 是ferd 大神 释出的一个 用于生产环境诊断Erlang 问题的一个工具, 不仅仅是对Erlang stdlib 接口的封装, 还有memory fragmentation 相关的函数. ...

  4. java接口入参模板化,适用于企业化服务远程调度模板化的场景,接口入参实现高度可配置化

    需求:远程服务接口模板化配置提供接入服务 模板接口分为三个模块:功能路由.参数校验.模板入库 路由:这里的实现方式很简单,就是根据业务标识找到对应的处理方法 参数校验: 参数校验这步涉及模板和校验类两 ...

  5. 安装一个Linux

    Linux--虚拟机的的安装: 首先需要一个可执行(VMware-workstation-full-14.1.2-8497320.exe)的文件和一个Linux(CentOS-7-x86_64-DVD ...

  6. jsp页面错误

    错误提示:The superclass javax.servlet.http.HttpServlet was not found on the Java Build Path. 解决办法:在build ...

  7. PFM 图片格式

    PFM  图片格式 参考:   https://linux.die.net/man/5/pfm 1. 描述 本文档描述了Netpbm转换器pamtopfm(1)和pfmtopam(1)所理解的PFM图 ...

  8. Python装饰器基础及运行时间

    一.装饰器基础 装饰器是可调用的对象,其参数是另一个函数(被装饰的函数).装饰器可能会处理被装饰的函数,然后把他返回,或者将其替换成另一个函数或可调用对象. eg:decorate装饰器 @decor ...

  9. mysql 判断某字段是否包含中文

    SELECT col FROM table WHERE LENGTH(col) != CHAR_LENGTH(col) LENGTH() 函数:返回字符串的长度,已字节符为单位 CHAR_LENGTH ...

  10. spring boot 常见的配置问题

    最近在自学spring boot ,新手教程网上很多,这里主要记录下配置过程中的一些疑难杂症.这些记录都是针对以下配置生成的项目 1.该项目一定要用jdk1.8 2.application.prope ...