Skip to content

Commit

Permalink
Add string map example
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikwidlund committed Jul 31, 2017
1 parent 78e0362 commit 5a81126
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Makefile.am
Expand Up @@ -2,7 +2,7 @@ ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4
AM_CFLAGS = -std=gnu11 -g -O3 -flto -fuse-linker-plugin
AM_LDFLAGS = -static

DIST_SUBDIRS = docs benchmark
DIST_SUBDIRS = docs benchmark examples

EXTRA_DIST = \
CHANGES \
Expand Down Expand Up @@ -36,7 +36,7 @@ mainheader_HEADERS = src/dynamic.h
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = libdynamic.pc

MAINTAINERCLEANFILES = aclocal.m4 config.h.in configure Makefile.in docs/Makefile.in benchmark/Makefile.in libdynamic-?.?.?.tar.gz
MAINTAINERCLEANFILES = aclocal.m4 config.h.in configure Makefile.in docs/Makefile.in benchmark/Makefile.in examples/Makefile.in libdynamic-?.?.?.tar.gz
maintainer-clean-local:; rm -rf autotools m4 libdynamic-?.?.?

### unit tests ###
Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Expand Up @@ -11,5 +11,5 @@ AC_PROG_LIBTOOL
AM_PROG_CC_C_O
AC_PROG_CXX

AC_CONFIG_FILES([Makefile docs/Makefile benchmark/Makefile libdynamic.pc])
AC_CONFIG_FILES([Makefile docs/Makefile benchmark/Makefile examples/Makefile libdynamic.pc])
AC_OUTPUT
16 changes: 16 additions & 0 deletions examples/Makefile.am
@@ -0,0 +1,16 @@
ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4
COMMON_FLAGS = -g -O3 -flto -fuse-linker-plugin -Isupport -I../src
AM_CFLAGS = -std=gnu11
AM_CXXFLAGS = -std=gnu++11
AM_CPPFLAGS = $(COMMON_FLAGS)
AM_LDFLAGS = $(COMMON_FLAGS)

bin_PROGRAMS = string_map

string_map_SOURCES = \
map_str_ptr.c \
string_map.c

string_map_LDADD = ../libdynamic.la

MAINTAINERCLEANFILES = Makefile.in
74 changes: 74 additions & 0 deletions examples/map_str_ptr.c
@@ -0,0 +1,74 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/param.h>
#include <time.h>
#include <err.h>

#include "dynamic.h"
#include "map_str_ptr.h"

struct map_str_ptr_element
{
char *key;
void *value;
};

static map_str_ptr_element empty = {.key = NULL};

static size_t hash(map *m, void *e)
{
(void) m;
return hash_string(((map_str_ptr_element *)e)->key);
}

static void set(map *m, void *dst, void *src)
{
map_str_ptr_element *d = dst, *s = src;

//printf("set %s\n", s->key ? s->key : "<null>");
(void) m;
*d = *s;
}

static int equal(map *m, void *p1, void *p2)
{
map_str_ptr_element *e1 = p1, *e2 = p2;
int p;

(void) m;
p = e1->key == e2->key || (e1->key && e2->key && strcmp(e1->key, e2->key) == 0);
//printf("%d %p ? %s == %s\n", p, e1, e1->key ? e1->key : "<null>", e2->key ? e2->key : "<null>");
return p;
}

void map_str_ptr_construct(map_str_ptr *m)
{
map_construct(m, sizeof(map_str_ptr_element), &empty, set);
}

void map_str_ptr_destruct(map_str_ptr *m)
{
map_destruct(m, NULL, NULL);
}

void *map_str_ptr_at(map_str_ptr *m, char *key)
{
return ((map_str_ptr_element *) map_at(m, (map_str_ptr_element[]){{.key = key}}, hash, equal))->value;
}

void map_str_ptr_insert(map_str_ptr *m, char *key, void *value)
{
map_insert(m,(map_str_ptr_element[]){{.key = key, .value = value}}, hash, equal, set, NULL);
}

void map_str_ptr_erase(map_str_ptr *m, char *key)
{
map_erase(m,(map_str_ptr_element[]){{.key = key}}, hash, equal, set, NULL);
}

size_t map_str_ptr_size(map_str_ptr *m)
{
return map_size(m);
}
14 changes: 14 additions & 0 deletions examples/map_str_ptr.h
@@ -0,0 +1,14 @@
#ifndef MAP_STR_PTR_H_INCLUDED
#define MAP_STR_PTR_H_INCLUDED

typedef map map_str_ptr;
typedef struct map_str_ptr_element map_str_ptr_element;

void map_str_ptr_construct(map_str_ptr *);
void map_str_ptr_destruct(map_str_ptr *);
void *map_str_ptr_at(map_str_ptr *, char *);
void map_str_ptr_insert(map_str_ptr *, char *, void *);
void map_str_ptr_erase(map_str_ptr *, char *);
size_t map_str_ptr_size(map_str_ptr *);

#endif /* MAP_STR_PTR_H_INCLUDED */
64 changes: 64 additions & 0 deletions examples/string_map.c
@@ -0,0 +1,64 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>

#include <dynamic.h>

#include "map_str_ptr.h"

typedef struct value value;
struct value
{
u_int number;
};

int main(int argc, char **argv)
{
char **keys, buffer[256];
value **values;
map_str_ptr m;
u_int n, i;

if (argc != 2)
exit(1);
n = strtoul(argv[1], NULL, 0);

// create keys/values
keys = calloc(n, sizeof keys[0]);
values = calloc(n, sizeof values[0]);
for (i = 0; i < n; i ++)
{
snprintf(buffer, sizeof buffer, "key-%u", i);
keys[i] = strdup(buffer);
values[i] = malloc(sizeof(value));
values[i]->number = i;
}

// construct map
map_str_ptr_construct(&m);

// insert key->value mappings
for (i = 0; i < n; i ++)
map_str_ptr_insert(&m, keys[i], values[i]);

// lookup key and validate value
for (i = 0; i < n; i ++)
{
snprintf(buffer, sizeof buffer, "key-%u", i);
assert(((value *) map_str_ptr_at(&m, buffer))->number == i);
}

// release keys/values
for (i = 0; i < n; i ++)
{
free(keys[i]);
free(values[i]);
}
free(keys);
free(values);

// destruct map
map_str_ptr_destruct(&m);
}

0 comments on commit 5a81126

Please sign in to comment.