Chinaunix首页 | 论坛 | 博客
  • 博客访问: 249041
  • 博文数量: 11
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 129
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-09 23:45
个人简介

新手上路

文章分类

全部博文(11)

文章存档

2014年(10)

2013年(1)

我的朋友

分类: C/C++

2014-03-13 15:35:41

* Generic hash function to calculate a hash code from the given string.
 *
 * For each byte of the string, this function left-shifts the value in an
 * accumulator and then adds the byte into the accumulator.  The contents of
 * the accumulator is returned after the entire string has been processed.
 * It is assumed that this result will be used as the "hashcode" parameter in
 * calls to other functions in this package.  These functions automatically
 * adjust the hashcode for the size of each hashtable.
 *
 * This algorithm probably works best when the hash table size is a prime
 * number.
 *
 * Hopefully, this function is better than the previous one which returned
 * the sum of the squares of all the bytes.  I'm still open to other
 * suggestions for a default hash function.  The programmer is more than
 * welcome to supply his/her own hash function as that is one of the design
 * features of this package.




点击(此处)折叠或打开

  1. unsigned long
  2. hash_HashFunction_s(const unsigned char *string, unsigned long len)
  3. {
  4.   register unsigned long accum; /*lint !e961*/

  5.   accum = 0;

  6.   for (; len > 0UL; len--)
  7.     {
  8.       accum <<= 1;
  9.       accum += (unsigned long) (*string++ & 0xFF);
  10.     }

  11.   return accum;
  12. }

阅读(865) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~