diff --git a/.gitignore b/.gitignore index 2168f0fd5..0a16f88aa 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ test-peek-file-reader test-parse-trie test-lrec test-multiple-containers +test-mlhmmv test-lrec-evaluators test_lrec_evaluators test-join-bucket-keeper @@ -69,6 +70,7 @@ test_lrec test_mlrutil test_mlrregex test_multiple_containers +test_mlhmmv test_parse_trie test_peek_file_reader test_string_builder diff --git a/c/Makefile.no-autoconfig b/c/Makefile.no-autoconfig index d6ce41081..687b58c00 100644 --- a/c/Makefile.no-autoconfig +++ b/c/Makefile.no-autoconfig @@ -140,7 +140,7 @@ TEST_MLHMMV_SRCS = \ lib/mlrutil.c \ lib/mlr_globals.c \ containers/mlhmmv.c \ - containers/sllv.c \ + containers/sllmv.c \ unit_test/test_mlhmmv.c TEST_MLRUTIL_SRCS = \ diff --git a/c/containers/Makefile.am b/c/containers/Makefile.am index 1c192b982..234e48474 100644 --- a/c/containers/Makefile.am +++ b/c/containers/Makefile.am @@ -41,6 +41,8 @@ libcontainers_la_SOURCES= \ slls.h \ sllv.c \ sllv.h \ + sllmv.c \ + sllmv.h \ top_keeper.c \ top_keeper.h libcontainers_la_LIBADD= \ diff --git a/c/containers/mlhmmv.c b/c/containers/mlhmmv.c index b69e5d660..a1ff685db 100644 --- a/c/containers/mlhmmv.c +++ b/c/containers/mlhmmv.c @@ -140,11 +140,13 @@ static void mlhmmv_level_free(mlhmmv_level_t* plevel) { //} // ---------------------------------------------------------------- -void mlhmmv_put(mlhmmv_t* pmap, sllv_t* pmkeys, mv_t* pvalue) { +void mlhmmv_put(mlhmmv_t* pmap, sllmv_t* pmkeys, mv_t* pvalue) { +} + +// level-put material: // if ((pmap->num_occupied + pmap->num_freed) >= (pmap->array_length*LOAD_FACTOR)) // mlhmmv_enlarge(pmap); // return mlhmmv_put_no_enlarge(pmap, key, pvvalue, free_flags); -} //static void* mlhmmv_put_no_enlarge(mlhmmv_t* pmap, slls_t* key, void* pvvalue, char free_flags) { // int index = mlhmmv_find_index_for_key(pmap, key); @@ -191,7 +193,7 @@ void mlhmmv_put(mlhmmv_t* pmap, sllv_t* pmkeys, mv_t* pvalue) { //} // ---------------------------------------------------------------- -mv_t* mlhmmv_get(mlhmmv_t* pmap, sllv_t* pmvkeys) { +mv_t* mlhmmv_get(mlhmmv_t* pmap, sllmv_t* pmvkeys) { // int index = mlhmmv_find_index_for_key(pmap, key); // mlhmmv_entry_t* pe = &pmap->entries[index]; // @@ -207,7 +209,7 @@ mv_t* mlhmmv_get(mlhmmv_t* pmap, sllv_t* pmvkeys) { } // ---------------------------------------------------------------- -int mlhmmv_has_keys(mlhmmv_t* pmap, sllv_t* pmvkeys) { +int mlhmmv_has_keys(mlhmmv_t* pmap, sllmv_t* pmvkeys) { // int index = mlhmmv_find_index_for_key(pmap, key); // // if (pmap->states[index] == OCCUPIED) @@ -306,3 +308,11 @@ int mlhmmv_has_keys(mlhmmv_t* pmap, sllv_t* pmvkeys) { // pe->ideal_index, key_string, value_string); // } //} + +// ---------------------------------------------------------------- +mlhmmv_value_t* mlhmmv_value_from_mv(mv_t* pmv) { + mlhmmv_value_t* pvalue = mlr_malloc_or_die(sizeof(mlhmmv_value_t)); + pvalue->type = MLHMMV_VALUE_TYPE_TERMINAL; + pvalue->u.mlrval = mv_copy(pmv); + return pvalue; +} diff --git a/c/containers/mlhmmv.h b/c/containers/mlhmmv.h index 84553ec67..a6d894e42 100644 --- a/c/containers/mlhmmv.h +++ b/c/containers/mlhmmv.h @@ -17,7 +17,7 @@ #define MLHMMV_H #include "containers/mlrval.h" -#include "containers/sllv.h" +#include "containers/sllmv.h" #define MLHMMV_VALUE_TYPE_TERMINAL 0xabcd #define MLHMMV_VALUE_TYPE_NON_TERMINAL 0xfedc @@ -61,9 +61,11 @@ typedef struct _mlhmmv_t { mlhmmv_t* mlhmmv_alloc(); void mlhmmv_free(mlhmmv_t* pmap); // pmkeys is a list of mlhmmv_value_t -void mlhmmv_put(mlhmmv_t* pmap, sllv_t* pmvkeys, mv_t* pvalue); -mv_t* mlhmmv_get(mlhmmv_t* pmap, sllv_t* pmvkeys); -int mlhmmv_has_keys(mlhmmv_t* pmap, sllv_t* pmvkeys); +void mlhmmv_put(mlhmmv_t* pmap, sllmv_t* pmvkeys, mv_t* pvalue); +mv_t* mlhmmv_get(mlhmmv_t* pmap, sllmv_t* pmvkeys); +int mlhmmv_has_keys(mlhmmv_t* pmap, sllmv_t* pmvkeys); + +mlhmmv_value_t* mlhmmv_value_from_mv(mv_t* pmv); //// Unit-test hook //int mlhmmv_check_counts(mlhmmv_t* pmap); diff --git a/c/containers/mlrval.h b/c/containers/mlrval.h index 294d19fdf..9c3595c6b 100644 --- a/c/containers/mlrval.h +++ b/c/containers/mlrval.h @@ -149,6 +149,12 @@ static inline mv_t mv_copy(mv_t* pval) { } } +static inline mv_t* mv_alloc_copy(mv_t* pold) { + mv_t* pnew = mlr_malloc_or_die(sizeof(mv_t)); + *pnew = mv_copy(pold); + return pnew; +} + // ---------------------------------------------------------------- static inline int mv_is_numeric(mv_t* pval) { return pval->type == MT_INT || pval->type == MT_FLOAT; diff --git a/c/containers/sllmv.c b/c/containers/sllmv.c new file mode 100644 index 000000000..34f398a2b --- /dev/null +++ b/c/containers/sllmv.c @@ -0,0 +1,67 @@ +#include "lib/mlrutil.h" +#include "containers/sllmv.h" + +// ---------------------------------------------------------------- +sllmv_t* sllmv_alloc() { + sllmv_t* plist = mlr_malloc_or_die(sizeof(sllmv_t)); + plist->phead = NULL; + plist->ptail = NULL; + plist->length = 0; + return plist; +} + +// ---------------------------------------------------------------- +void sllmv_free(sllmv_t* plist) { + if (plist == NULL) + return; + sllmve_t* pnode = plist->phead; + while (pnode != NULL) { + sllmve_t* pdel = pnode; + pnode = pnode->pnext; + mv_free(pdel->pvalue); + free(pdel); + } + plist->phead = NULL; + plist->ptail = 0; + plist->length = 0; + + free(plist); +} + +// ---------------------------------------------------------------- +void sllmv_add(sllmv_t* plist, mv_t* pvalue) { + sllmve_t* pnode = mlr_malloc_or_die(sizeof(sllmve_t)); + pnode->pvalue = mv_alloc_copy(pvalue); + if (plist->ptail == NULL) { + pnode->pnext = NULL; + plist->phead = pnode; + plist->ptail = pnode; + } else { + pnode->pnext = NULL; + plist->ptail->pnext = pnode; + plist->ptail = pnode; + } + plist->length++; +} + +// ---------------------------------------------------------------- +sllmv_t* sllmv_single(mv_t* pvalue) { + sllmv_t* psllmv = sllmv_alloc(); + sllmv_add(psllmv, pvalue); + return psllmv; +} + +sllmv_t* sllmv_double(mv_t* pvalue1, mv_t* pvalue2) { + sllmv_t* psllmv = sllmv_alloc(); + sllmv_add(psllmv, pvalue1); + sllmv_add(psllmv, pvalue2); + return psllmv; +} + +sllmv_t* sllmv_triple(mv_t* pvalue1, mv_t* pvalue2, mv_t* pvalue3) { + sllmv_t* psllmv = sllmv_alloc(); + sllmv_add(psllmv, pvalue1); + sllmv_add(psllmv, pvalue2); + sllmv_add(psllmv, pvalue3); + return psllmv; +} diff --git a/c/containers/sllmv.h b/c/containers/sllmv.h new file mode 100644 index 000000000..d55d01c8f --- /dev/null +++ b/c/containers/sllmv.h @@ -0,0 +1,31 @@ +// ================================================================ +// Singly-linked list of mlrval, with tail for append. +// Everything is copied; nothing is referenced. +// ================================================================ + +#ifndef SLLMV_H +#define SLLMV_H + +#include "mlrval.h" + +typedef struct _sllmve_t { + mv_t* pvalue; + struct _sllmve_t *pnext; +} sllmve_t; + +typedef struct _sllmv_t { + sllmve_t *phead; + sllmve_t *ptail; + int length; +} sllmv_t; + +sllmv_t* sllmv_alloc(); +void sllmv_free(sllmv_t* plist); +void sllmv_add(sllmv_t* plist, mv_t* pvalue); + +sllmv_t* sllmv_single(mv_t* pvalue); +sllmv_t* sllmv_double(mv_t* pvalue1, mv_t* pvalue2); +sllmv_t* sllmv_triple(mv_t* pvalue1, mv_t* pvalue2, mv_t* pvalue3); + +#endif // SLLMV_H + diff --git a/c/unit_test/test_mlhmmv.c b/c/unit_test/test_mlhmmv.c index 314a70f4a..417c2ef5c 100644 --- a/c/unit_test/test_mlhmmv.c +++ b/c/unit_test/test_mlhmmv.c @@ -13,6 +13,18 @@ int assertions_failed = 0; static char* test_stub() { mu_assert_lf(0 == 0); + mlhmmv_t* pmap = mlhmmv_alloc(); + + mv_t key = mv_from_int(3LL); + sllmv_t* pmkeys = sllmv_single(&key); + mv_t value = mv_from_int(4LL); + + mlhmmv_put(pmap, pmkeys, &value); + + sllmv_free(pmkeys); + + mlhmmv_free(pmap); + return NULL; }