前面的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. 7.8 GRASP原则八: 间接 Indirection

    GRASP原则八: 间接 Indirection  若两个对象直接连接,导致耦合太紧,如何解决?3.1 GRASP rule8: Indirection 间接  Name: Indirection ...

  2. 【转】ASP.NET Core开发之HttpContext

    ASP.NET Core中的HttpContext开发,在ASP.NET开发中我们总是会经常用到HttpContext. 那么在ASP.NET Core中要如何使用HttpContext呢,下面就来具 ...

  3. 生成器yield

    #!/usr/bin/python#coding=utf-8'''Created on 2017年11月23日 from home @author: James zhan ''' def counte ...

  4. [Codeforces778E]Selling Numbers

    Problem 给一个由问号和数字组成的数字串A(问号表示任一数字). 再给定n个数字Bi,和0~9的数字的价值. F(x)表示x各个位数上的价值和.问A为何值时,sum(F(Bi+A))的值最大为多 ...

  5. leetcode python 032 识别最长合法括号

    # 给定一个只包含字符'('和')'的字符串,# 找到最长的有效(格式良好)括号子字符串的长度.# 对于“(()”,最长的有效括号子串是“()”,其长度为2.# 另一个例子是“)()())”,其中最长 ...

  6. VS遇到的问题记录

    Q:id为xxxx的进程当前未运行 A:将端口改掉.

  7. 5.使用std的迭代器访问并修改图像

    void Test_ColorReduceByIterator() { Mat g_srcImage=imread("D:\\OpenCV Projects\\OpenCV_Test_Ima ...

  8. windows mysql 的myini

    地址 :C:\ProgramData\MySQL\MySQL Server 5.7

  9. Alpha冲刺8

    前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/10023260.html 作业博客:https://edu.cnblogs.com/campus ...

  10. 剑指Offer 27. 字符串的排列 (字符串)

    题目描述 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入描述: 输 ...