Add hashing.
This commit is contained in:
parent
6f0f55365b
commit
0ed91e92d1
7 changed files with 109 additions and 4 deletions
|
@ -9,6 +9,7 @@ SRCS = array.c \
|
||||||
string.c
|
string.c
|
||||||
|
|
||||||
INCLUDES = ${SRCS:.c=.h} \
|
INCLUDES = ${SRCS:.c=.h} \
|
||||||
corefw.h
|
corefw.h \
|
||||||
|
hash.h
|
||||||
|
|
||||||
include ../buildsys.mk
|
include ../buildsys.mk
|
||||||
|
|
19
src/array.c
19
src/array.c
|
@ -29,6 +29,7 @@
|
||||||
|
|
||||||
#include "object.h"
|
#include "object.h"
|
||||||
#include "array.h"
|
#include "array.h"
|
||||||
|
#include "hash.h"
|
||||||
|
|
||||||
struct CFWArray {
|
struct CFWArray {
|
||||||
CFWObject obj;
|
CFWObject obj;
|
||||||
|
@ -88,6 +89,23 @@ equal(void *ptr1, void *ptr2)
|
||||||
return true;
|
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*
|
static void*
|
||||||
copy(void *ptr)
|
copy(void *ptr)
|
||||||
{
|
{
|
||||||
|
@ -258,6 +276,7 @@ static CFWClass class = {
|
||||||
.ctor = ctor,
|
.ctor = ctor,
|
||||||
.dtor = dtor,
|
.dtor = dtor,
|
||||||
.equal = equal,
|
.equal = equal,
|
||||||
|
.hash = hash,
|
||||||
.copy = copy
|
.copy = copy
|
||||||
};
|
};
|
||||||
CFWClass *cfw_array = &class;
|
CFWClass *cfw_array = &class;
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
typedef struct CFWClass {
|
typedef struct CFWClass {
|
||||||
|
@ -37,6 +38,7 @@ typedef struct CFWClass {
|
||||||
bool (*ctor)(void*, va_list args);
|
bool (*ctor)(void*, va_list args);
|
||||||
void (*dtor)(void*);
|
void (*dtor)(void*);
|
||||||
bool (*equal)(void*, void*);
|
bool (*equal)(void*, void*);
|
||||||
|
uint32_t (*hash)(void*);
|
||||||
void* (*copy)(void*);
|
void* (*copy)(void*);
|
||||||
} CFWClass;
|
} CFWClass;
|
||||||
|
|
||||||
|
|
52
src/hash.h
Normal file
52
src/hash.h
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2012, Jonathan Schleifer <js@webkeks.org>
|
||||||
|
* 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
|
13
src/object.c
13
src/object.c
|
@ -95,6 +95,17 @@ cfw_equal(void *ptr1, void *ptr2)
|
||||||
return (obj1 == obj2);
|
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*
|
void*
|
||||||
cfw_copy(void *ptr)
|
cfw_copy(void *ptr)
|
||||||
{
|
{
|
||||||
|
@ -102,7 +113,7 @@ cfw_copy(void *ptr)
|
||||||
|
|
||||||
if (obj->cls->copy != NULL)
|
if (obj->cls->copy != NULL)
|
||||||
return obj->cls->copy(obj);
|
return obj->cls->copy(obj);
|
||||||
else
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,7 @@ extern void* cfw_ref(void*);
|
||||||
extern void cfw_unref(void*);
|
extern void cfw_unref(void*);
|
||||||
extern void cfw_free(void*);
|
extern void cfw_free(void*);
|
||||||
extern bool cfw_equal(void*, void*);
|
extern bool cfw_equal(void*, void*);
|
||||||
|
extern uint32_t cfw_hash(void*);
|
||||||
extern void* cfw_copy(void*);
|
extern void* cfw_copy(void*);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
19
src/string.c
19
src/string.c
|
@ -30,6 +30,7 @@
|
||||||
|
|
||||||
#include "object.h"
|
#include "object.h"
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
|
#include "hash.h"
|
||||||
|
|
||||||
struct CFWString {
|
struct CFWString {
|
||||||
CFWObject obj;
|
CFWObject obj;
|
||||||
|
@ -83,6 +84,23 @@ equal(void *ptr1, void *ptr2)
|
||||||
return !memcmp(str1->data, str2->data, str1->len);
|
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*
|
static void*
|
||||||
copy(void *ptr)
|
copy(void *ptr)
|
||||||
{
|
{
|
||||||
|
@ -177,6 +195,7 @@ static CFWClass class = {
|
||||||
.ctor = ctor,
|
.ctor = ctor,
|
||||||
.dtor = dtor,
|
.dtor = dtor,
|
||||||
.equal = equal,
|
.equal = equal,
|
||||||
|
.hash = hash,
|
||||||
.copy = copy
|
.copy = copy
|
||||||
};
|
};
|
||||||
CFWClass *cfw_string = &class;
|
CFWClass *cfw_string = &class;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue