CoreFW  Check-in [a2613e09be]

Overview
Comment:Add hashing.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: a2613e09be8f9cef27f9995c1fd96ea82feea6349f3d84c4edf8cc22a0491289
User & Date: js on 2012-04-09 14:06:27
Other Links: manifest | tags
Context
2012-04-22
02:34
ctor: Set pointers to NULL before allocation. check-in: bdb850f3e2 user: js tags: trunk
2012-04-09
14:06
Add hashing. check-in: a2613e09be user: js tags: trunk
2012-04-08
21:10
Add cfw_string_find(). check-in: 8f1f410978 user: js tags: trunk
Changes

Modified src/Makefile from [af64497e3d] to [b46de62f05].

1
2
3
4
5
6
7
8
9
10
11
12

13
14
SHARED_LIB = ${LIB_PREFIX}corefw${LIB_SUFFIX}
LIB_MAJOR = 0
LIB_MINOR = 0

SRCS = array.c	\
       class.c	\
       object.c	\
       range.c	\
       string.c

INCLUDES = ${SRCS:.c=.h} \
	   corefw.h


include ../buildsys.mk










|
|
>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SHARED_LIB = ${LIB_PREFIX}corefw${LIB_SUFFIX}
LIB_MAJOR = 0
LIB_MINOR = 0

SRCS = array.c	\
       class.c	\
       object.c	\
       range.c	\
       string.c

INCLUDES = ${SRCS:.c=.h}	\
	   corefw.h		\
	   hash.h

include ../buildsys.mk

Modified src/array.c from [0c97dcdd1f] to [203632d233].

25
26
27
28
29
30
31

32
33
34
35
36
37
38
 */

#include <stdlib.h>
#include <stdint.h>

#include "object.h"
#include "array.h"


struct CFWArray {
	CFWObject obj;
	void **data;
	size_t size;
};








>







25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 */

#include <stdlib.h>
#include <stdint.h>

#include "object.h"
#include "array.h"
#include "hash.h"

struct CFWArray {
	CFWObject obj;
	void **data;
	size_t size;
};

83
84
85
86
87
88
89

















90
91
92
93
94
95
96

	for (i = 0; i < array1->size; i++)
		if (cfw_equal(array1->data[i], array2->data[i]))
			return false;

	return true;
}


















static void*
copy(void *ptr)
{
	CFWArray *array = ptr;
	CFWArray *new;
	size_t i;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

	for (i = 0; i < array1->size; i++)
		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;
	CFWArray *new;
	size_t i;
254
255
256
257
258
259
260

261
262
263

static CFWClass class = {
	.name = "CFWArray",
	.size = sizeof(CFWArray),
	.ctor = ctor,
	.dtor = dtor,
	.equal = equal,

	.copy = copy
};
CFWClass *cfw_array = &class;







>



272
273
274
275
276
277
278
279
280
281
282

static CFWClass class = {
	.name = "CFWArray",
	.size = sizeof(CFWArray),
	.ctor = ctor,
	.dtor = dtor,
	.equal = equal,
	.hash = hash,
	.copy = copy
};
CFWClass *cfw_array = &class;

Modified src/class.h from [f56d4a2a72] to [263717fe7a].

25
26
27
28
29
30
31

32
33
34
35
36
37
38
39

40
41
42
43
44
45
 */

#ifndef __COREFW_CLASS_H__
#define __COREFW_CLASS_H__

#include <stddef.h>
#include <stdbool.h>

#include <stdarg.h>

typedef struct CFWClass {
	const char *name;
	size_t size;
	bool (*ctor)(void*, va_list args);
	void (*dtor)(void*);
	bool (*equal)(void*, void*);

	void* (*copy)(void*);
} CFWClass;

extern const char* cfw_class_name(CFWClass*);

#endif







>








>






25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 */

#ifndef __COREFW_CLASS_H__
#define __COREFW_CLASS_H__

#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdarg.h>

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*);

#endif

Added src/hash.h version [5088531f7f].









































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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

Modified src/object.c from [45025da8da] to [997da41042].

90
91
92
93
94
95
96











97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
	CFWObject *obj1 = ptr1, *obj2 = ptr2;

	if (obj1->cls->equal != NULL) {
		return obj1->cls->equal(obj1, obj2);
	} else
		return (obj1 == obj2);
}












void*
cfw_copy(void *ptr)
{
	CFWObject *obj = ptr;

	if (obj->cls->copy != NULL)
		return obj->cls->copy(obj);
	else
		return NULL;
}

static CFWClass class = {
	.name = "CFWObject",
	.size = sizeof(CFWObject),
};
CFWClass *cfw_object = &class;







>
>
>
>
>
>
>
>
>
>
>








|
|







90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
	CFWObject *obj1 = ptr1, *obj2 = ptr2;

	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);

	return NULL;
}

static CFWClass class = {
	.name = "CFWObject",
	.size = sizeof(CFWObject),
};
CFWClass *cfw_object = &class;

Modified src/object.h from [39453636ba] to [dc1cf1f6a1].

36
37
38
39
40
41
42

43
44
45

extern CFWClass *cfw_object;
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 void* cfw_copy(void*);

#endif







>



36
37
38
39
40
41
42
43
44
45
46

extern CFWClass *cfw_object;
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

Modified src/string.c from [33cb49edea] to [415ab5e653].

26
27
28
29
30
31
32

33
34
35
36
37
38
39

#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#include "object.h"
#include "string.h"


struct CFWString {
	CFWObject obj;
	char *data;
	size_t len;
};








>







26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#include "object.h"
#include "string.h"
#include "hash.h"

struct CFWString {
	CFWObject obj;
	char *data;
	size_t len;
};

78
79
80
81
82
83
84

















85
86
87
88
89
90
91
	str2 = ptr2;

	if (str1->len != str2->len)
		return false;

	return !memcmp(str1->data, str2->data, str1->len);
}


















static void*
copy(void *ptr)
{
	CFWString *str = ptr;
	CFWString *new;








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
	str2 = ptr2;

	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;
	CFWString *new;

173
174
175
176
177
178
179

180
181
182

static CFWClass class = {
	.name = "CFWString",
	.size = sizeof(CFWString),
	.ctor = ctor,
	.dtor = dtor,
	.equal = equal,

	.copy = copy
};
CFWClass *cfw_string = &class;







>



191
192
193
194
195
196
197
198
199
200
201

static CFWClass class = {
	.name = "CFWString",
	.size = sizeof(CFWString),
	.ctor = ctor,
	.dtor = dtor,
	.equal = equal,
	.hash = hash,
	.copy = copy
};
CFWClass *cfw_string = &class;