-/* $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
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);
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
-/* $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
#include <string.h>
#include <zebra_strmap.h>
#include <yaz/nmem.h>
+#include <yaz/xmalloc.h>
struct strmap_entry {
char *name;
NMEM nmem_str;
NMEM nmem_ent;
int hsize;
+ int size;
struct strmap_entry **entries;
struct strmap_entry *free_entries;
};
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++)
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,
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
-/* $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
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);
}
}