Add hashing.

This commit is contained in:
Jonathan Schleifer 2012-04-09 16:06:27 +02:00
parent 6f0f55365b
commit 0ed91e92d1
7 changed files with 109 additions and 4 deletions

View file

@ -30,6 +30,7 @@
#include "object.h"
#include "string.h"
#include "hash.h"
struct CFWString {
CFWObject obj;
@ -83,6 +84,23 @@ equal(void *ptr1, void *ptr2)
return !memcmp(str1->data, str2->data, str1->len);
}
static uint32_t
hash(void *ptr)
{
CFWString *str = ptr;
size_t i;
uint32_t hash;
CFW_HASH_INIT(hash);
for (i = 0; i < str->len; i++)
CFW_HASH_ADD(hash, str->data[i]);
CFW_HASH_FINALIZE(hash);
return hash;
}
static void*
copy(void *ptr)
{
@ -177,6 +195,7 @@ static CFWClass class = {
.ctor = ctor,
.dtor = dtor,
.equal = equal,
.hash = hash,
.copy = copy
};
CFWClass *cfw_string = &class;