Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
78e0362
commit 5a81126
Showing
6 changed files
with
171 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |