哈希函数的优化

youhua.h

# pragma once
# include<stdio.h>
# include<string.h>
# include<stdlib.h>
# include<assert.h>
#define MAX_SIZE 10
typedef int DataType;
typedef enum{
 EXIST, EMPTY, DELETE
}State;
typedef struct HTElem
{
 DataType _data;
 State _state;
}HTElem;
typedef struct HashTable
{
 HTElem *_array;//改为动态
 int _capacity;
  int _size;//计算哈希表中有效元素的个数
 int _IsLineDetective;//是否为线性探测
}HashTable, HT;
void HashTableInit(HT *ht, int capacity,int IsLineDetetive);
void HashTableInsert(HT *ht, DataType data);
int CheckCapacity(HT *ht);
void HashTableDestroy(HT *ht);


youhua.c
# include"youhua.h"
void HashTableInit(HT *ht, int capacity, int IsLineDetetive)
{
 int i = 0;
 ht->_array = (HTElem*)malloc(capacity*sizeof(HTElem));
 if (NULL == ht->_array)
 {
  assert(0);
  return;
 }
 //把位置给成空
 for (; i < capacity; ++i)
  ht->_array[i]._state = EMPTY;
 ht->_size = 0;
 ht->_capacity = capacity;
 ht->_IsLineDetective = IsLineDetetive;
}
void HashTableInsert(HT *ht, DataType data)
{
 int hashAddr = -1;
 int i = 0;
 assert(ht);
 if (!CheckCapacity(ht))
  return;
 HashTableDestroy(&data);
 //计算哈希地址
 hashAddr = HashFunc(data);
}
int CheckCapacity(HT *ht)
{
 assert(ht);
 //开辟新空间
 if (ht->_size * 10 / ht->_capacity >= 7)//判断是否增容
 {
  int NewCapacity = ht->_capacity * 2;
  HT NewHt;
  int i = 0;
  HashTableInit((&NewHt), NewCapacity, ht->_IsLineDetective);
  //拷贝元素(旧插入新)
  for (; i < ht->_size; ++i)
  {
   if (ht->_array[i]._state == EXIST)
    HashTableInsert(&NewHt, ht->_array[i]._data);
  }
  Swap(ht, &NewHt);
  HashTableDestroy(&NewHt);
 }
 return 1;
}
void Swap(HT *lht, HT *rht)
{
 int tmp;
 assert(lht);
 assert(rht);
 //交换capacity
 tmp = lht->_capacity;
 lht->_capacity = rht->_capacity;
 rht->_capacity = tmp;
 //交换size
 tmp = lht->_size;
 lht->_size = rht->_size;
 rht->_size = tmp;
 //交换IsSetective
 tmp = lht->_IsLineDetective;
 lht->_IsLineDetective = rht->_IsLineDetective;
 rht->_IsLineDetective = tmp;
 //交换array
 tmp = (int)lht->_array;
 lht->_array = rht->_array;
 rht->_array = (HTElem*)tmp;
}
void HashTableDestroy(HT *ht)
{
 assert(ht);
 if (ht->_array)
 {
  free(ht->_array);
  ht->_array = NULL;
  ht->_capacity = 0;
  ht->_size = 0;
 }
}

Common.h

# pragma once
# include"youhua.h"
typedef unsigned int size_t;
#define _PrimeSize 28;//enum:_PrimeSize =28};
//素数
size_t GetNextPrime(int capacity);
void HashTableInitYouHua(HT *ht, int capacity,int IsLineDetetive);
int CheckCapacity(HT *ht);
//字符串哈希算法
int HashFunc(DataType data);
static unsigned int SortToInt(const char * str);

Common.c

# include"Common.h"
// 使用素数表对齐做哈希表的容量,降低哈希冲突
const unsigned long _PrimeList[_PrimeSize] =
{
 53ul, 97ul, 193ul, 389ul, 769ul,
 1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
 49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
 1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
 50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457u
 161061271lul, 3221225473ul, 429496729lul
};
size_t GetNextPrime(int capacity)
{
 int i = 0;
 for (; i < _PrimeSize; ++i)
 {
  if (_PrimeList[i]>capacity)
   return _PrimeList[i];
 }
 return _PrimeList[_PrimeSize - 1];
}
void HashTableInitYouHua(HT *ht, int capacity, int IsLineDetetive)
{
 int i = 0;
 capacity = GetNextPrime(capacity);
 ht->_array = (HTElem*)malloc(capacity*sizeof(HTElem));
 if (NULL == ht->_array)
 {
  assert(0);
  return;
 }
}
int CheckCapacity(HT *ht)
{
 assert(ht);
 //开辟新空间
 if (ht->_size * 10 / ht->_capacity >= 7)//判断是否增容
 {
  int NewCapacity = GetNextPrime(ht->_capacity);
  HT NewHt;
  int i = 0;
  HashTableInit((&NewHt), NewCapacity, ht->_IsLineDetective);
  //拷贝元素(旧插入新)
  for (; i < ht->_size; ++i)
  {
   if (ht->_array[i]._state == EXIST)
    HashTableInsert(&NewHt, ht->_array[i]._data);
  }
  Swap(ht, &NewHt);
  HashTableDestroy(&NewHt);
 }
 return 1;
}
//字符串哈希算法
int HashFunc(DataType data)
{
 return StrToInt(data) % MAX_SIZE;
}
int DetectiveLine(int hashAddr)
{
 hashAddr++;
 //hashAddr %= MAX_SIZE;
 if (hashAddr == MAX_SIZE)
  hashAddr = 0;
 return hashAddr;
}
static unsigned int StrToInt(const char * str)
{
 unsigned int seed = 131; // 31 131 1313 13131 131313
 unsigned int hash = 0;
 while (*str)
 {
  hash = hash * seed + (*str++);
 }
 return (hash & 0x7FFFFFFF);
}

void TestHashTable()
{
 HashTable ht;
 HashTableInit(&ht, 10, 1);
 HashTableInsert(&ht, "欧阳锋");
 HashTableInsert(&ht, "李某某");
 HashTableInsert(&ht, "哈哈");
 HashTableInsert(&ht, "会话");
}


阅读更多
博主设置当前文章不允许评论。

字符串哈希函数设计

2014年05月11日 1.32MB 下载

毕业论文:哈希函数的构造方法

2010年05月07日 117KB 下载

没有更多推荐了,返回首页

不良信息举报

哈希函数的优化

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭