我正在尝试为字符串设想一个好的哈希函数.而且我认为总结字符串中前五个字符的unicode值可能是一个好主意(假设它有五个,否则在它结束时停止).这是一个好主意,还是一个坏主意?
我在Java中这样做,但我不认为这会产生很大的不同.
在C中有一个switch构造,它使一个人能够根据一个测试整数值执行不同的条件代码分支,例如,
int a;
/* Read the value of "a" from some source, e.g. user input */
switch ( a ) {
case 100:
// Code
break;
case 200:
// Code
break;
default:
// Code
break;
}
Run Code Online (Sandbox Code Playgroud)
如何为字符串值获得相同的行为(即避免所谓的" if- else阶梯"),即char *?
我在用
unordered_map<string, int>
Run Code Online (Sandbox Code Playgroud)
和
unordered_map<int, int>
Run Code Online (Sandbox Code Playgroud)
在每种情况下使用什么散列函数以及在每种情况下碰撞的可能性是多少?我将分别在每种情况下插入唯一字符串和唯一int作为键.
我很想知道在字符串和int键及其碰撞统计数据的情况下哈希函数的算法.
我不能使用boost:hash因为我必须坚持使用C而不能使用C++.
但是,我需要散列大量(10K到100k)的令牌字符串(长度为5到40个字节),以便在这些字符串中搜索最快.
MD5,SHA1或任何长哈希函数对于一个简单的任务来说似乎太重了,我没有做加密.此外还有存储和计算成本.
因此我的问题是:
什么是最简单的哈希算法,可以确保在大多数实际情况下防止碰撞.
哈希值要使用多少位?我正在为32位系统开发.Perl/Python中的哈希算法是否也使用32位哈希?或者我必须跳到64?
关于常见脚本语言中哈希表的实现:实现是否检查冲突,还是可以完全避免该部分?
我正在创建一个类似于a的结构String,除了它只处理Unicode UTF-32标量值.因此,它是一个数组UInt32.(有关更多背景,请参阅此问题.)
我希望能够将自定义ScalarString结构用作字典中的键.例如:
var suffixDictionary = [ScalarString: ScalarString]() // Unicode key, rendered glyph value
// populate dictionary
suffixDictionary[keyScalarString] = valueScalarString
// ...
// check if dictionary contains Unicode scalar string key
if let renderedSuffix = suffixDictionary[unicodeScalarString] {
// do something with value
}
Run Code Online (Sandbox Code Playgroud)
为此,ScalarString需要实现Hashable协议.我以为我可以这样做:
struct ScalarString: Hashable {
private var scalarArray: [UInt32] = []
var hashValue : Int {
get {
return self.scalarArray.hashValue // error
} …Run Code Online (Sandbox Code Playgroud) 我们目前正在处理我班级的哈希函数.我们的教练要求我们在互联网上使用哈希函数来比较我们在代码中使用的哈希函数.
第一个:
int HashTable::hash (string word)
// POST: the index of entry is returned
{ int sum = 0;
for (int k = 0; k < word.length(); k++)
sum = sum + int(word[k]);
return sum % SIZE;
}
Run Code Online (Sandbox Code Playgroud)
第二:
int HashTable::hash (string word)
{
int seed = 131;
unsigned long hash = 0;
for(int i = 0; i < word.length(); i++)
{
hash = (hash * seed) + word[i];
}
return hash % SIZE;
}
Run Code Online (Sandbox Code Playgroud)
SIZE为501(哈希表的大小),输入来自20,000多个单词的文本文件.
我用一些代码示例看到了这个问题,但是并不确定在哈希函数中要查找什么.如果我理解正确,在我的情况下,哈希采用输入(字符串)并进行数学计算以为字符串分配数字并将其插入表中.这个过程是为了提高搜索列表的速度吗?
如果我的逻辑是合理的,有没有人有一个很好的例子或资源显示一个涉及字符串的不同哈希函数?甚至是编写我自己的高效哈希函数的过程.
我试图在C中实现一个不使用查找表的CRC32算法(我需要在没有足够内存可用的引导加载程序中使用它).有没有可用的公共许可解决方案吗?
unsigned int HashString( const char *string ) {
const char* p;
unsigned hash = 40503;
for ( p = string; *p != '\0'; ++p ) {
hash += *p;
hash += ( hash << 10 );
hash ^= ( hash >> 6 );
}
hash += ( hash << 3 );
hash ^= ( hash >> 11 );
hash += ( hash << 15 );
return hash;
}
Run Code Online (Sandbox Code Playgroud)
只是徘徊在他们的代码上.虽然我以前从未见过像这样的散列函数.
当谈到按位运算时,我并不太专业,我知道位移和屏蔽是如何工作的,但只是在检查位是否设置的基本情况下.
这到底是做什么的?
从这个答案,我看到了这个:
unsigned long
hash(unsigned char *str)
{
unsigned long hash = 5381;
int c;
while (c = *str++)
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
Run Code Online (Sandbox Code Playgroud)
问题是我没有看到while停止执行的方式.一般来说,作业不是真的吗?我的意思if(a = 5)是真的.
编译器也给了我这个警告:
warning: suggest parentheses around assignment used as truth value [-Wparentheses]
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我关于循环的确切情况吗?我强烈建议一个例子是这里最好的方法.
括号还应该在哪里使这个更清楚?