From 0ed91e92d19c33b8dc620d27a0e9e9c4914a8066 Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Mon, 9 Apr 2012 16:06:27 +0200 Subject: [PATCH] Add hashing. --- src/Makefile | 5 +++-- src/array.c | 19 +++++++++++++++++++ src/class.h | 2 ++ src/hash.h | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/object.c | 15 +++++++++++++-- src/object.h | 1 + src/string.c | 19 +++++++++++++++++++ 7 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 src/hash.h diff --git a/src/Makefile b/src/Makefile index ca8c824..61719ab 100644 --- a/src/Makefile +++ b/src/Makefile @@ -8,7 +8,8 @@ SRCS = array.c \ range.c \ string.c -INCLUDES = ${SRCS:.c=.h} \ - corefw.h +INCLUDES = ${SRCS:.c=.h} \ + corefw.h \ + hash.h include ../buildsys.mk diff --git a/src/array.c b/src/array.c index d50d79a..d8dbf45 100644 --- a/src/array.c +++ b/src/array.c @@ -29,6 +29,7 @@ #include "object.h" #include "array.h" +#include "hash.h" struct CFWArray { CFWObject obj; @@ -88,6 +89,23 @@ equal(void *ptr1, void *ptr2) return true; } +static uint32_t +hash(void *ptr) +{ + CFWArray *array = ptr; + size_t i; + uint32_t hash; + + CFW_HASH_INIT(hash); + + for (i = 0; i < array->size; i++) + CFW_HASH_ADD_HASH(hash, cfw_hash(array->data[i])); + + CFW_HASH_FINALIZE(hash); + + return hash; +} + static void* copy(void *ptr) { @@ -258,6 +276,7 @@ static CFWClass class = { .ctor = ctor, .dtor = dtor, .equal = equal, + .hash = hash, .copy = copy }; CFWClass *cfw_array = &class; diff --git a/src/class.h b/src/class.h index fc021ff..f00d066 100644 --- a/src/class.h +++ b/src/class.h @@ -29,6 +29,7 @@ #include #include +#include #include typedef struct CFWClass { @@ -37,6 +38,7 @@ typedef struct CFWClass { bool (*ctor)(void*, va_list args); void (*dtor)(void*); bool (*equal)(void*, void*); + uint32_t (*hash)(void*); void* (*copy)(void*); } CFWClass; diff --git a/src/hash.h b/src/hash.h new file mode 100644 index 0000000..3cd77dd --- /dev/null +++ b/src/hash.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2012, Jonathan Schleifer + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __COREFW_HASH_H__ +#define __COREFW_HASH_H__ + +#define CFW_HASH_INIT(hash) hash = 0 +#define CFW_HASH_ADD(hash, byte) \ + { \ + hash += (uint8_t)byte; \ + hash += (hash << 10); \ + hash ^= (hash >> 6); \ + } +#define CFW_HASH_FINALIZE(hash) \ + { \ + hash += (hash << 3); \ + hash ^= (hash >> 11); \ + hash += (hash << 15); \ + } +#define CFW_HASH_ADD_HASH(hash, other_) \ + { \ + uint32_t other = other_; \ + CFW_HASH_ADD(hash, (other >> 24) & 0xFF); \ + CFW_HASH_ADD(hash, (other >> 16) & 0xFF); \ + CFW_HASH_ADD(hash, (other >> 8) & 0xFF); \ + CFW_HASH_ADD(hash, other & 0xFF); \ + } + +#endif diff --git a/src/object.c b/src/object.c index 251bc18..bc53a50 100644 --- a/src/object.c +++ b/src/object.c @@ -95,6 +95,17 @@ cfw_equal(void *ptr1, void *ptr2) return (obj1 == obj2); } +uint32_t +cfw_hash(void *ptr) +{ + CFWObject *obj = ptr; + + if (obj->cls->hash != NULL) + return obj->cls->hash(obj); + + return (uint32_t)(uintptr_t)ptr; +} + void* cfw_copy(void *ptr) { @@ -102,8 +113,8 @@ cfw_copy(void *ptr) if (obj->cls->copy != NULL) return obj->cls->copy(obj); - else - return NULL; + + return NULL; } static CFWClass class = { diff --git a/src/object.h b/src/object.h index db8778f..c5bdac3 100644 --- a/src/object.h +++ b/src/object.h @@ -40,6 +40,7 @@ extern void* cfw_ref(void*); extern void cfw_unref(void*); extern void cfw_free(void*); extern bool cfw_equal(void*, void*); +extern uint32_t cfw_hash(void*); extern void* cfw_copy(void*); #endif diff --git a/src/string.c b/src/string.c index 407ea89..4d90680 100644 --- a/src/string.c +++ b/src/string.c @@ -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;