From 08d6561cdd2bb03f02913f25b20a428aa9aea575 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Mon, 3 Dec 2007 09:12:38 +0000 Subject: [PATCH] Added zebra_strmap_it(erator). --- include/zebra_strmap.h | 14 ++++++++++- util/strmap.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++-- util/test_strmap.c | 35 ++++++++++++++++++++++----- 3 files changed, 101 insertions(+), 9 deletions(-) diff --git a/include/zebra_strmap.h b/include/zebra_strmap.h index 3bf0960..9440e98 100644 --- a/include/zebra_strmap.h +++ b/include/zebra_strmap.h @@ -1,4 +1,4 @@ -/* $Id: zebra_strmap.h,v 1.1 2007-12-02 11:30:28 adam Exp $ +/* $Id: zebra_strmap.h,v 1.2 2007-12-03 09:12:38 adam Exp $ Copyright (C) 1995-2007 Index Data ApS @@ -28,6 +28,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA YAZ_BEGIN_CDECL typedef struct zebra_strmap *zebra_strmap_t; +typedef struct zebra_strmap_it_s *zebra_strmap_it; YAZ_EXPORT zebra_strmap_t zebra_strmap_create(void); @@ -46,6 +47,17 @@ void *zebra_strmap_lookup(zebra_strmap_t st, const char *name, int no, YAZ_EXPORT int zebra_strmap_remove(zebra_strmap_t st, const char *name); +YAZ_EXPORT +zebra_strmap_it zebra_strmap_it_create(zebra_strmap_t st); + +YAZ_EXPORT +void zebra_strmap_it_destroy(zebra_strmap_it it); + +YAZ_EXPORT +const char *zebra_strmap_it_next(zebra_strmap_it it, void **data_buf, + size_t *data_len); + + YAZ_END_CDECL #endif diff --git a/util/strmap.c b/util/strmap.c index 8f0b237..1d054ff 100644 --- a/util/strmap.c +++ b/util/strmap.c @@ -1,4 +1,4 @@ -/* $Id: strmap.c,v 1.1 2007-12-02 11:30:28 adam Exp $ +/* $Id: strmap.c,v 1.2 2007-12-03 09:12:38 adam Exp $ Copyright (C) 1995-2007 Index Data ApS @@ -24,6 +24,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #include #include +#include struct strmap_entry { char *name; @@ -36,6 +37,7 @@ struct zebra_strmap { NMEM nmem_str; NMEM nmem_ent; int hsize; + int size; struct strmap_entry **entries; struct strmap_entry *free_entries; }; @@ -48,6 +50,7 @@ zebra_strmap_t zebra_strmap_create(void) st->nmem_ent = nmem_ent; st->nmem_str = nmem_create(); st->hsize = 1001; + st->size = 0; st->free_entries = 0; st->entries = nmem_malloc(nmem_ent, st->hsize * sizeof(*st->entries)); for (i = 0; i < st->hsize; i++) @@ -90,6 +93,7 @@ void zebra_strmap_add(zebra_strmap_t st, const char *name, ne->data_buf = nmem_malloc(st->nmem_str, data_len); memcpy(ne->data_buf, data_buf, data_len); ne->data_len = data_len; + (st->size)++; } void *zebra_strmap_lookup(zebra_strmap_t st, const char *name, int no, @@ -122,11 +126,64 @@ int zebra_strmap_remove(zebra_strmap_t st, const char *name) tmp->next = st->free_entries; st->free_entries = tmp; + + --(st->size); return 1; } return 0; } - + +int zebra_strmap_get_size(zebra_strmap_t st) +{ + return st->size; +} + +struct zebra_strmap_it_s { + int hno; + struct strmap_entry *ent; + zebra_strmap_t st; + +}; + +zebra_strmap_it zebra_strmap_it_create(zebra_strmap_t st) +{ + zebra_strmap_it it = (zebra_strmap_it) xmalloc(sizeof(*it)); + it->hno = 0; + it->ent = 0; + it->st = st; + return it; +} + +void zebra_strmap_it_destroy(zebra_strmap_it it) +{ + xfree(it); +} + +const char *zebra_strmap_it_next(zebra_strmap_it it, void **data_buf, + size_t *data_len) +{ + struct strmap_entry *ent = 0; + while (!it->ent && it->hno < it->st->hsize) + { + it->ent = it->st->entries[it->hno]; + it->hno++; + } + if (it->ent) + { + ent = it->ent; + it->ent = ent->next; + } + if (ent) + { + if (data_buf) + *data_buf = ent->data_buf; + if (data_len) + *data_len = ent->data_len; + return ent->name; + } + return 0; +} + /* * Local variables: * c-basic-offset: 4 diff --git a/util/test_strmap.c b/util/test_strmap.c index c376bec..422c1a3 100644 --- a/util/test_strmap.c +++ b/util/test_strmap.c @@ -1,4 +1,4 @@ -/* $Id: test_strmap.c,v 1.1 2007-12-02 11:30:28 adam Exp $ +/* $Id: test_strmap.c,v 1.2 2007-12-03 09:12:38 adam Exp $ Copyright (C) 1995-2007 Index Data ApS @@ -33,20 +33,43 @@ static void test1(void) zebra_strmap_destroy(sm); } { - int v1 = 1; - int *data_buf; + int v = 1; + void *data_buf; size_t data_len; zebra_strmap_t sm = zebra_strmap_create(); YAZ_CHECK(!zebra_strmap_lookup(sm, "a", 0, 0)); - zebra_strmap_add(sm, "a", &v1, sizeof v1); + zebra_strmap_add(sm, "a", &v, sizeof v); data_buf = zebra_strmap_lookup(sm, "a", 0, &data_len); - YAZ_CHECK(data_buf && data_len == sizeof v1 - && v1 == *((int*) data_buf)); + YAZ_CHECK(data_buf && data_len == sizeof v + && v == *((int*) data_buf)); zebra_strmap_remove(sm, "a"); data_buf = zebra_strmap_lookup(sm, "a", 0, &data_len); YAZ_CHECK(data_buf == 0); + + v = 1; + zebra_strmap_add(sm, "a", &v, sizeof v); + + v = 2; + zebra_strmap_add(sm, "b", &v, sizeof v); + + v = 3; + zebra_strmap_add(sm, "c", &v, sizeof v); + + { + zebra_strmap_it it = zebra_strmap_it_create(sm); + const char *name; + int no = 0; + while ((name = zebra_strmap_it_next(it, &data_buf, &data_len))) + { + YAZ_CHECK(!strcmp(name, "a") || !strcmp(name, "b") || + !strcmp(name, "c")); + no++; + } + YAZ_CHECK_EQ(no, 3); + zebra_strmap_it_destroy(it); + } zebra_strmap_destroy(sm); } } -- 1.7.10.4