Skip to content

Commit 89e46a2

Browse files
author
Fredrik Widlund
committed
hash map refactoring
1 parent ba34ba8 commit 89e46a2

18 files changed

Lines changed: 205 additions & 354 deletions

File tree

CHANGES

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,13 @@ Released 2017-12-17
1414

1515
- List type
1616
- More uniform interfaces
17+
18+
Version 1.2
19+
===========
20+
21+
Released 2019-04-19
22+
23+
* New features:
24+
25+
- Add maps (string map) and mapi (uint64_t) abstractions
26+
- Refactor map interface

Makefile.am

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@ src/dynamic/buffer.c \
1515
src/dynamic/list.c \
1616
src/dynamic/vector.c \
1717
src/dynamic/string.c \
18-
src/dynamic/map.c
18+
src/dynamic/map.c \
19+
src/dynamic/maps.c \
20+
src/dynamic/mapi.c
1921

2022
HEADER_FILES = \
2123
src/dynamic/hash.h \
2224
src/dynamic/buffer.h \
2325
src/dynamic/list.h \
2426
src/dynamic/vector.h \
2527
src/dynamic/string.h \
26-
src/dynamic/map.h
28+
src/dynamic/map.h \
29+
src/dynamic/maps.h \
30+
src/dynamic/mapi.h
2731

2832
AUTOMAKE_OPTIONS = subdir-objects
2933
lib_LTLIBRARIES= libdynamic.la
@@ -43,14 +47,13 @@ maintainer-clean-local:; rm -rf autotools m4 libdynamic-?.?.?
4347

4448
### unit tests ###
4549

46-
CHECK_CFLAGS = -std=gnu11 -O0 -g -ftest-coverage -fprofile-arcs
47-
CHECK_LDADD = -L. -ldynamic_test -lcmocka
48-
CHECK_LDFLAGS_EXTRA = -Wl,--wrap=malloc -Wl,--wrap=calloc -Wl,--wrap=realloc -Wl,--wrap=aligned_alloc -Wl,--wrap=abort
49-
5050
check_LIBRARIES = libdynamic_test.a
5151
libdynamic_test_a_CFLAGS = $(CHECK_CFLAGS)
5252
libdynamic_test_a_SOURCES = $(SOURCE_FILES) $(HEADER_FILES)
5353

54+
CHECK_CFLAGS = -std=gnu11 -O0 -g -ftest-coverage -fprofile-arcs
55+
CHECK_LDADD = libdynamic_test.a -lcmocka
56+
CHECK_LDFLAGS_EXTRA = -Wl,--wrap=malloc -Wl,--wrap=calloc -Wl,--wrap=realloc -Wl,--wrap=aligned_alloc -Wl,--wrap=abort
5457

5558
check_PROGRAMS = test/hash
5659
test_hash_CFLAGS = $(CHECK_CFLAGS)
@@ -82,6 +85,18 @@ test_string_LDADD = $(CHECK_LDADD)
8285
test_string_LDFLAGS = $(CHECK_LDFLAGS_EXTRA)
8386
test_string_SOURCES = test/string.c test/mock.c
8487

88+
check_PROGRAMS += test/maps
89+
test_maps_CFLAGS = $(CHECK_CFLAGS)
90+
test_maps_LDADD = $(CHECK_LDADD)
91+
test_maps_LDFLAGS = $(CHECK_LDFLAGS_EXTRA)
92+
test_maps_SOURCES = test/maps.c test/mock.c
93+
94+
check_PROGRAMS += test/mapi
95+
test_mapi_CFLAGS = $(CHECK_CFLAGS)
96+
test_mapi_LDADD = $(CHECK_LDADD)
97+
test_mapi_LDFLAGS = $(CHECK_LDFLAGS_EXTRA)
98+
test_mapi_SOURCES = test/mapi.c test/mock.c
99+
85100
check_PROGRAMS += test/map
86101
test_map_CFLAGS = $(CHECK_CFLAGS)
87102
test_map_LDADD = $(CHECK_LDADD)

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
AC_INIT([libdynamic], [1.1.0], [fredrik.widlund@gmail.com])
1+
AC_INIT([libdynamic], [1.2.0], [fredrik.widlund@gmail.com])
22
AC_CONFIG_AUX_DIR(autotools)
33
AC_CONFIG_MACRO_DIR([m4])
44
AM_INIT_AUTOMAKE([-Wall -Werror foreign no-define])

examples/Makefile.am

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ AM_CXXFLAGS = -std=gnu++11
55
AM_CPPFLAGS = $(COMMON_FLAGS)
66
AM_LDFLAGS = $(COMMON_FLAGS)
77

8-
bin_PROGRAMS = string_map list
8+
bin_PROGRAMS = maps mapi list
99

10-
string_map_SOURCES = \
11-
map_str_ptr.c \
12-
string_map.c
10+
maps_SOURCES = \
11+
maps.c
1312

14-
string_map_LDADD = ../libdynamic.la
13+
maps_LDADD = ../libdynamic.la
14+
15+
mapi_SOURCES = \
16+
mapi.c
17+
18+
mapi_LDADD = ../libdynamic.la
1519

1620
list_SOURCES = \
1721
list.c

examples/string_map.c

Lines changed: 0 additions & 79 deletions
This file was deleted.

src/dynamic.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#ifndef DYNAMIC_H_INCLUDED
22
#define DYNAMIC_H_INCLUDED
33

4-
#define DYNAMIC_VERSION "1.1.0"
4+
#define DYNAMIC_VERSION "1.2.0"
55
#define DYNAMIC_VERSION_MAJOR 1
6-
#define DYNAMIC_VERSION_MINOR 1
6+
#define DYNAMIC_VERSION_MINOR 2
77
#define DYNAMIC_VERSION_PATCH 0
88

99
#ifdef __cplusplus
@@ -16,6 +16,8 @@ extern "C" {
1616
#include <dynamic/vector.h>
1717
#include <dynamic/string.h>
1818
#include <dynamic/map.h>
19+
#include <dynamic/maps.h>
20+
#include <dynamic/mapi.h>
1921

2022
#ifdef __cplusplus
2123
}

src/dynamic/hash.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,13 @@ uint64_t hash_string(char *s)
220220
{
221221
return hash_data(s, strlen(s));
222222
}
223+
224+
uint64_t hash_uint64(uint64_t v)
225+
{
226+
v ^= v >> 33;
227+
v *= 0xff51afd7ed558ccdULL;
228+
v ^= v >> 33;
229+
v *= 0xc4ceb9fe1a85ec53ULL;
230+
v ^= v >> 33;
231+
return v;
232+
}

src/dynamic/hash.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33

44
uint64_t hash_string(char *);
55
uint64_t hash_data(void *, size_t);
6+
uint64_t hash_uint64(uint64_t);
67

78
#endif /* HASH_H_INCLUDED */

src/dynamic/list.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void list_construct(list *l)
3838
l->previous = (list_item *) l;
3939
}
4040

41-
void list_destruct(list *l, list_release_callback *release)
41+
void list_destruct(list *l, list_release *release)
4242
{
4343
list_clear(l, release);
4444
}
@@ -104,7 +104,7 @@ void list_insert(void *list_object, void *object, size_t size)
104104
item->list.next->list.previous = item;
105105
}
106106

107-
void list_erase(void *object, list_release_callback *release)
107+
void list_erase(void *object, list_release *release)
108108
{
109109
list_item *item = list_object_item(object);
110110

@@ -117,15 +117,15 @@ void list_erase(void *object, list_release_callback *release)
117117
free(item);
118118
}
119119

120-
void list_clear(list *l, list_release_callback *release)
120+
void list_clear(list *l, list_release *release)
121121
{
122122
while (!list_empty(l))
123123
list_erase(list_front(l), release);
124124
}
125125

126126
/* operations */
127127

128-
void *list_find(list *l, list_compare_callback *compare, void *object)
128+
void *list_find(list *l, list_compare *compare, void *object)
129129
{
130130
void *list_object;
131131

src/dynamic/list.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#define list_foreach(l, o) for ((o) = list_front(l); (o) != list_end(l); (o) = list_next(o))
55
#define list_foreach_reverse(l, o) for ((o) = list_back(l); (o) != list_end(l); (o) = list_previous(o))
66

7-
typedef void list_release_callback(void *);
8-
typedef int list_compare_callback(void *, void *);
7+
typedef void list_release(void *);
8+
typedef int list_compare(void *, void *);
99
typedef struct list_item list_item;
1010
typedef struct list list;
1111

@@ -23,7 +23,7 @@ struct list_item
2323

2424
/* constructor/destructor */
2525
void list_construct(list *);
26-
void list_destruct(list *, list_release_callback *);
26+
void list_destruct(list *, list_release *);
2727

2828
/* iterators */
2929
void *list_next(void *);
@@ -41,10 +41,10 @@ void *list_end(list *);
4141
void list_push_front(list *, void *, size_t);
4242
void list_push_back(list *, void *, size_t);
4343
void list_insert(void *, void *, size_t);
44-
void list_erase(void *, list_release_callback *);
45-
void list_clear(list *, list_release_callback *);
44+
void list_erase(void *, list_release *);
45+
void list_clear(list *, list_release *);
4646

4747
/* operations */
48-
void *list_find(list *, list_compare_callback *, void *);
48+
void *list_find(list *, list_compare *, void *);
4949

5050
#endif /* LIST_H_INCLUDED */

0 commit comments

Comments
 (0)