Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -6,9 +6,10 @@ class.c \ object.c \ range.c \ string.c -INCLUDES = ${SRCS:.c=.h} \ - corefw.h +INCLUDES = ${SRCS:.c=.h} \ + corefw.h \ + hash.h include ../buildsys.mk Index: src/array.c ================================================================== --- src/array.c +++ src/array.c @@ -27,10 +27,11 @@ #include #include #include "object.h" #include "array.h" +#include "hash.h" struct CFWArray { CFWObject obj; void **data; size_t size; @@ -85,10 +86,27 @@ if (cfw_equal(array1->data[i], array2->data[i])) return false; 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) { CFWArray *array = ptr; @@ -256,8 +274,9 @@ .name = "CFWArray", .size = sizeof(CFWArray), .ctor = ctor, .dtor = dtor, .equal = equal, + .hash = hash, .copy = copy }; CFWClass *cfw_array = &class; Index: src/class.h ================================================================== --- src/class.h +++ src/class.h @@ -27,18 +27,20 @@ #ifndef __COREFW_CLASS_H__ #define __COREFW_CLASS_H__ #include #include +#include #include typedef struct CFWClass { const char *name; size_t size; bool (*ctor)(void*, va_list args); void (*dtor)(void*); bool (*equal)(void*, void*); + uint32_t (*hash)(void*); void* (*copy)(void*); } CFWClass; extern const char* cfw_class_name(CFWClass*); ADDED src/hash.h Index: src/hash.h ================================================================== --- /dev/null +++ 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 Index: src/object.c ================================================================== --- src/object.c +++ src/object.c @@ -92,20 +92,31 @@ if (obj1->cls->equal != NULL) { return obj1->cls->equal(obj1, obj2); } else 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) { CFWObject *obj = ptr; if (obj->cls->copy != NULL) return obj->cls->copy(obj); - else - return NULL; + + return NULL; } static CFWClass class = { .name = "CFWObject", .size = sizeof(CFWObject), Index: src/object.h ================================================================== --- src/object.h +++ src/object.h @@ -38,8 +38,9 @@ extern void* cfw_new(CFWClass*, ...); 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 Index: src/string.c ================================================================== --- src/string.c +++ src/string.c @@ -28,10 +28,11 @@ #include #include #include "object.h" #include "string.h" +#include "hash.h" struct CFWString { CFWObject obj; char *data; size_t len; @@ -80,10 +81,27 @@ if (str1->len != str2->len) return false; 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) { CFWString *str = ptr; @@ -175,8 +193,9 @@ .name = "CFWString", .size = sizeof(CFWString), .ctor = ctor, .dtor = dtor, .equal = equal, + .hash = hash, .copy = copy }; CFWClass *cfw_string = &class;