简体   繁体   English

Map C ++的哈希函数

[英]Hashing Function for Map C++

I am trying to determine a hash function which takes an input (i, k) and determines a unique solution. 我试图确定一个散列函数,该函数接受输入(i,k)并确定唯一的解决方案。

The possible inputs for (i, k) range from 0 to 100. Consider each (i, k) as a position of a node in a trinomial tree. (i,k)的可能输入范围是0到100。将每个(i,k)视为三叉树中节点的位置。

Ex: (0, 0) can diverge to (1, 1) (1, 0) (1, -1). 
    (1, 1) can diverge to (2, 2) (2, 1) (2, 0).

Sample given here: 此处给出的示例:

http://www.google.com/imgres?imgurl=http://sfb649.wiwi.hu-berlin.de/fedc_homepage/xplore/tutorials/stfhtmlimg1156.gif&imgrefurl=http://sfb649.wiwi.hu-berlin.de/fedc_homepage/xplore/tutorials/stfhtmlnode41.html&h=413&w=416&sz=4&tbnid=OegDZu-yeVitZM:&tbnh=90&tbnw=91&zoom=1&usg=__9uQWDNYNLV14YioWWbrqPgfa3DQ=&docid=2hhitNyRWjI_DM&hl=en&sa=X&ei=xAfFUIbyG8nzyAHv2YDICg&ved=0CDsQ9QEwAQ

I am using a map 我正在使用地图

map <double, double> hash_table

I need a key value to be determined from pairs (i, k) to hash to to value for that (i, k) 我需要从对(i,k)中确定一个键值以将其散列为该值(i,k)

So far I was only able to come up with linear functions such as: double Hash_function(int i, int k) 到目前为止,我只能提出线性函数,例如:double Hash_function(int i,int k)

{
    //double val = pow(i, k) + i;
    //return (val % 4294967296);
    return (i*3.1415 + k*i*9.12341); 
}

However, I cannot determine a unique key with a certain (i, k). 但是,我无法确定具有(i,k)的唯一键。 What kind of functions can I use to help me do so? 我可以使用什么样的功能来帮助我?

Mathematically speaking, you are seeking a bijection . 从数学上讲,您正在寻求双射 This is not a hash function in the computer science sense because hash functions are expected to produce collisions on occasion (unless it is a perfect hash function ). 这不是计算机科学意义上的哈希函数,因为预计哈希函数有时会产生冲突(除非它是理想的哈希函数 )。

What you have labeled hash_table is not a hash table. 您标记为hash_table的不是哈希表。 std::map is a different data structure called an ordered map, and it is able to use any key type for which the less-than operator < provides a strict weak ordering . std::map是一种不同的数据结构,称为有序映射,它可以使用小于运算符<提供严格弱排序的任何键类型。 You can, in fact, use std::pair from the utility header: 实际上,您可以从utility头中使用std::pair

std::map<std::pair<int, int>, double> table;

To insert into the table, you would use: 要插入表中,可以使用:

table[std::make_pair(i, j)] = value;

@Grizzly is correct that using double as a key is problematic. @Grizzly是正确的,使用double作为键是有问题的。 Maybe it would be better to use a string-based hashing technique? 也许使用基于字符串的哈希技术会更好?

If i & k are 0-100, than your key can be a simple short (even signed) and is uniquely generated with something like short key = i | k << 8 如果i&k为0-100,则您的密钥可以是简单的短键(甚至带符号),并且是唯一生成的,例如short key = i | k << 8 short key = i | k << 8 . short key = i | k << 8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM