(Boolan)STL与泛型编程学习笔记(第五周)

1.一个万用的hash function

在之前的课程中,我们知道以Hash Table为底层的容器过程(如unordered_map),在使用过程中,必须要有一个hash function来为每一个元素生成一个hash code作为元素在哈希表中的key,也就是元素在哈希表中的具体位置。对于一些build-in类型(比如字符串),标准库自带hash function,但是对于自定义类型来说,这个函数该如何定义?我们能否找到一个通用的方法,实现hash code的计算呢?

自定义类型,都是由基本类型组成,我们可以将它其中的各个基本数据类型分开计算出,然后将其相加(当然这是比较天真的方法)。先看看这种方法的实现代码:

[cpp]view plaincopyprint?

classCustomerHash

{

public:

std::size_toperator()(constCustomer& c)const{

returnstd::hash()(c.fname)

+ std::hash()(c.Iname)

+ std::hash

}

}

class CustomerHash

{

public:

std::size_t operator()(const Customer& c) const{

return std::hash()(c.fname)

+ std::hash()(c.Iname)

+ std::hash

}

}

这个方法可以实现出计算Hash code,但是因为这个方法只是简单的相加hash code,因此hash code的重复概率比较高进而会导致篮子中的元素过多,影响查询的效率。

在了解其他方法之前,先介绍一下hash function的三种定义型式:

型式1:

[cpp]view plaincopyprint?

#include

classCustomer{

//........

};

classCustomerHash

{

public:

std::size_toperator()(constCustomer& c)const{

return/*........*/;

}

};

unordered_set customers;

#include

class Customer{

//........

};

class CustomerHash

{

public:

std::size_t operator()(const Customer& c) const{

return /*........*/;

}

};

unordered_set customers;

型式2:

[cpp]view plaincopyprint?

size_tcustomer_hash_func(constCustomer& c)

{

return/*......*/;

}

unorder_set customers(20, customer_hash_func);

size_t customer_hash_func(const Customer& c)

{

return /*......*/;

}

unorder_set customers(20, customer_hash_func);

型式3:

通过偏特化来实现

[cpp]view plaincopyprint?

classMyString

{

private:

char* _data;

size_t_len;

};

namespacestd;

{

template<>

structhash

{

size_toperatoe()(constMyString& s)constnoexcept{

returnhash()(string(s.get()));

}

}

}

class MyString

{

private:

char* _data;

size_t _len;

};

namespace std;

{

template<>

struct hash

{

size_t operatoe()(const MyString& s) const noexcept{

return hash()(string(s.get()));

}

}

}

通过以上三种型式可以指定我们需要的hash function,但是能否能有一个万用的hash function来实现自定义类型的hash code的计算?

在C++ TR1版本及以后,STL为我们提供了一个万用的hash function,它是如何实现的呢?

具体调用代码如下:

[cpp]view plaincopyprint?

classCustomerHash

{

public:

std::size_toperator()(constCunstomer& c)const{

returnhash_val(c.fname, c,Iname, c.no);

}

}

class CustomerHash

{

public:

std::size_t operator()(const Cunstomer& c) const {

return hash_val(c.fname, c,Iname, c.no);

}

}

接下来看看它的实现代码,具体如下:

[cpp]view plaincopyprint?

//auxiliary generic funtions

template

inlinesize_thash_val(constTypes&... args){

size_tseed = 0;

hash_val(seed, args...);

returnseed;

}

template

inlinevoidhash_val(size_t& seed,constT& val,constType&... args){

hash_combine(seed, val);

hash_val(seed, args...);

}

#include

template

inlinevoidhash_combine(size_t& seed,constT& val){

seed = std::hash(val) + 0x9e3779b9

+ (seed << 6) + (seed >> 2);

}

//auxiliary generic funtions

template

inlinevoidhash_val(size_t& seed,constT& val){

hash_combine(seed, val);

}

//auxiliary generic funtions

template

inline size_t hash_val(const Types&... args){

size_t seed = 0;

hash_val(seed, args...);

return seed;

}

template

inline void hash_val(size_t& seed, const T& val, const Type&... args){

hash_combine(seed, val);

hash_val(seed, args...);

}

#include

template

inline void hash_combine(size_t& seed, const T& val){

seed = std::hash(val) + 0x9e3779b9

+ (seed << 6) + (seed >> 2);

}

//auxiliary generic funtions

template

inline void hash_val(size_t& seed, const T& val){

hash_combine(seed, val);

}

这种方法和之前提到的简单相加的方法相比,更加的巧妙,它使用了C++11中的variadic templates,可以传入多个模板,传入函数中的每个参数都有一个模板,对不同类型的参数会有不同的解决方案,也就是会传入不同的函数。

由上图可知,在①中加入了seed(最终被视为hash code),从而使得模板变成1+n的形式,通过递归调用②中的hash_val函数,不断调用④中的hash_combine函数来改变seed,同时减少接收的参数,最终递归结束时变成1+1的形式,调用③中的hash_val函数,也会调用④中的hash_combine函数,最终确认seed值,也就是算出最后的hash code。

其中④中的hash_combine函数中的0x9e3779b9属于黄金比例中的一部分:

2.tuple

tuple是元之组合,数之组合的意思,它是C++2.0之后引进的一种存放各种不同类型元素的集合。

tuple的使用方法如下:

tuple实现的原理,以Gnu4.8为例:

它是通过继承的方法来不断地剔除第一个参数,最终来实现对每一个元素的操作。

3.type traits

type traits(类型萃取机)能有效地分辨类是否具有某种类型,通过调用它我们可以实现对不同的类型指定不同的操作。

在Gnu2.9中的实现代码如下:

[cpp]view plaincopyprint?

struct__true_type{};

struct__false_type{};

//泛化

template

struct__type_traits{

typedef__true_type this_dummy_member_must_be_first;

typedef__false_type has_trivial_default_constructor;

typedef__false_type has_trivial_copy_constructor;

typedef__false_type has_trivial_assignment_operator;

typedef__false_type has_trivial_destructor;

typedef__false_type is_POD_type;//POD = Plain Old Data,代表旧式的class 也就是struct

};

//int的特化

template<>

struct__type_traits{

typedef__true_type has_trivial_default_constructor;

typedef__true_type has_trivial_copy_constructor;

typedef__true_type has_trivial_assignment_operator;

typedef__true_type has_trivial_destructor;

typedef__true_type is_POD_type;

}

//double的特化

template<>

struct__type_traits{

typedef__true_type has_trivial_default_constructor;

typedef__true_type has_trivial_copy_constructor;

typedef__true_type has_trivial_assignment_operator;

typedef__true_type has_trivial_destructor;

typedef__true_type is_POD_type;

}

struct __true_type{};

struct __false_type{};

//泛化

template

struct __type_traits{

typedef __true_type this_dummy_member_must_be_first;

typedef __false_type has_trivial_default_constructor;

typedef __false_type has_trivial_copy_constructor;

typedef __false_type has_trivial_assignment_operator;

typedef __false_type has_trivial_destructor;

typedef __false_type is_POD_type;  //POD = Plain Old Data,代表旧式的class 也就是struct

};

//int的特化

template<>

struct __type_traits{

typedef __true_type has_trivial_default_constructor;

typedef __true_type has_trivial_copy_constructor;

typedef __true_type has_trivial_assignment_operator;

typedef __true_type has_trivial_destructor;

typedef __true_type is_POD_type;

}

//double的特化

template<>

struct __type_traits{

typedef __true_type has_trivial_default_constructor;

typedef __true_type has_trivial_copy_constructor;

typedef __true_type has_trivial_assignment_operator;

typedef __true_type has_trivial_destructor;

typedef __true_type is_POD_type;

}

上面的type traits是依靠模板的泛化和特化的版本来实现。

从C++11开始,type traits的特性变得更为强大和复杂。

type traits的实现

万变不离其宗,通过模板的泛化和特化,我们可以实现各种操作。

(1)is_void

(2)is_integral

(3)is_class,is_union,is_enum,is_pod

(4)is_move_assignable

4.cout

Gnu2.9:

Gnu4.9:

5.moveable元素对容器的影响

5.1对vctor影响

5.2对list影响

5.3对deque影响

5.4对multiset影响

5.5对unordered_multiset影响

5.6写一个moveable class

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,110评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,443评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,474评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,881评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,902评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,698评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,418评论 3 419
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,332评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,796评论 1 316
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,968评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,110评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,792评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,455评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,003评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,130评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,348评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,047评论 2 355

推荐阅读更多精彩内容