这是在通用链表的基础上实现的集合,关于链表的实现参见:http://blog.youkuaiyun.com/swwlqw/article/details/22498833。
注意集合中只存储了指针,没有储存实际的数据。
对于新的数据类型来说,需要自定义HashCode函数和equal函数。
下面还给出了几个常见的hashCode函数和equal函数。
(1)HashCode函数
头文件
/*************************
*** File myHashCode.h
**************************/
#ifndef MYHASHCODE_H_INCLUDED
#define MYHASHCODE_H_INCLUDED
#include <string.h>
#define HASHCODE_MULT 31
//默认的hashCode
int myHashCodeDefault(void * a);
//int类型hashCode
int myHashCodeInt(void * a);
//char类型的hashCode
int myHashCodeChar(void * a);
//string类型的hashCode
int myHashCodeString(void * a);
#endif // MYHASHCODE_H_INCLUDED
源文件
/*************************
*** File myHashCode.c
**************************/
#include "myHashCode.h"
//默认的hashCode
int myHashCodeDefault(void * a)
{
return (int) a;
}
//int类型hashCode
int myHashCodeInt(void * a)
{
int * aa = (int *) a;
return *aa;
}
//char类型的hashCode
int myHashCodeChar(void * a)
{
char *aa = (char *) a;
return *aa;
}
//string类型的hashCode
int myHashCodeString(void * a)
{
int re = 0;
char *aa = (char *) a;
while (*aa)
{
re += HASHCODE_MULT * *aa;
aa++;
}
return re;
}