diff --git a/c/mapping/mapper_step.c b/c/mapping/mapper_step.c index 602252385..f46534154 100644 --- a/c/mapping/mapper_step.c +++ b/c/mapping/mapper_step.c @@ -6,6 +6,7 @@ #include "lib/mlr_globals.h" #include "containers/sllv.h" #include "containers/slls.h" +#include "containers/string_array.h" #include "containers/lhmslv.h" #include "containers/lhmsv.h" #include "containers/mixutil.h" @@ -26,8 +27,9 @@ typedef step_t* step_alloc_func_t(char* input_field_name); typedef struct _mapper_step_state_t { slls_t* pstepper_names; - slls_t* pvalue_field_names; - slls_t* pgroup_by_field_names; + string_array_t* pvalue_field_names; // parameter + string_array_t* pvalue_field_values; // scratch space used per-record + slls_t* pgroup_by_field_names; // parameter lhmslv_t* groups; } mapper_step_state_t; @@ -56,7 +58,8 @@ typedef struct _mapper_step_state_t { // ---------------------------------------------------------------- static void mapper_step_usage(FILE* o, char* argv0, char* verb); static mapper_t* mapper_step_parse_cli(int* pargi, int argc, char** argv); -static mapper_t* mapper_step_alloc(slls_t* pstepper_names, slls_t* pvalue_field_names, slls_t* pgroup_by_field_names); +static mapper_t* mapper_step_alloc(slls_t* pstepper_names, string_array_t* pvalue_field_names, + slls_t* pgroup_by_field_names); static void mapper_step_free(void* pvstate); static sllv_t* mapper_step_process(lrec_t* pinrec, context_t* pctx, void* pvstate); @@ -101,16 +104,16 @@ static void mapper_step_usage(FILE* o, char* argv0, char* verb) { } static mapper_t* mapper_step_parse_cli(int* pargi, int argc, char** argv) { - slls_t* pstepper_names = NULL; - slls_t* pvalue_field_names = NULL; - slls_t* pgroup_by_field_names = slls_alloc(); + slls_t* pstepper_names = NULL; + string_array_t* pvalue_field_names = NULL; + slls_t* pgroup_by_field_names = slls_alloc(); char* verb = argv[(*pargi)++]; ap_state_t* pstate = ap_alloc(); - ap_define_string_list_flag(pstate, "-a", &pstepper_names); - ap_define_string_list_flag(pstate, "-f", &pvalue_field_names); - ap_define_string_list_flag(pstate, "-g", &pgroup_by_field_names); + ap_define_string_list_flag(pstate, "-a", &pstepper_names); + ap_define_string_array_flag(pstate, "-f", &pvalue_field_names); + ap_define_string_list_flag(pstate, "-g", &pgroup_by_field_names); if (!ap_parse(pstate, verb, pargi, argc, argv)) { mapper_step_usage(stderr, argv[0], verb); @@ -126,13 +129,16 @@ static mapper_t* mapper_step_parse_cli(int* pargi, int argc, char** argv) { } // ---------------------------------------------------------------- -static mapper_t* mapper_step_alloc(slls_t* pstepper_names, slls_t* pvalue_field_names, slls_t* pgroup_by_field_names) { +static mapper_t* mapper_step_alloc(slls_t* pstepper_names, string_array_t* pvalue_field_names, + slls_t* pgroup_by_field_names) +{ mapper_t* pmapper = mlr_malloc_or_die(sizeof(mapper_t)); mapper_step_state_t* pstate = mlr_malloc_or_die(sizeof(mapper_step_state_t)); pstate->pstepper_names = pstepper_names; pstate->pvalue_field_names = pvalue_field_names; + pstate->pvalue_field_values = string_array_alloc(pvalue_field_names->length); pstate->pgroup_by_field_names = pgroup_by_field_names; pstate->groups = lhmslv_alloc(); @@ -146,7 +152,8 @@ static mapper_t* mapper_step_alloc(slls_t* pstepper_names, slls_t* pvalue_field_ static void mapper_step_free(void* pvstate) { mapper_step_state_t* pstate = pvstate; slls_free(pstate->pstepper_names); - slls_free(pstate->pvalue_field_names); + string_array_free(pstate->pvalue_field_names); + string_array_free(pstate->pvalue_field_values); slls_free(pstate->pgroup_by_field_names); // xxx free the level-2's 1st lhmslv_free(pstate->groups); @@ -159,14 +166,12 @@ static sllv_t* mapper_step_process(lrec_t* pinrec, context_t* pctx, void* pvstat return sllv_single(NULL); // ["s", "t"] - slls_t* pvalue_field_values = mlr_selected_values_from_record(pinrec, pstate->pvalue_field_names); + mlr_reference_values_from_record(pinrec, pstate->pvalue_field_names, pstate->pvalue_field_values); slls_t* pgroup_by_field_values = mlr_selected_values_from_record(pinrec, pstate->pgroup_by_field_names); - if (pvalue_field_values == NULL || pgroup_by_field_values == NULL) { - slls_free(pvalue_field_values); + if (pgroup_by_field_values == NULL) { slls_free(pgroup_by_field_values); - lrec_free(pinrec); - return NULL; + return sllv_single(pinrec); } lhmsv_t* group_to_acc_field = lhmslv_get(pstate->groups, pgroup_by_field_values); @@ -175,12 +180,14 @@ static sllv_t* mapper_step_process(lrec_t* pinrec, context_t* pctx, void* pvstat lhmslv_put(pstate->groups, slls_copy(pgroup_by_field_values), group_to_acc_field); } - sllse_t* pa = pstate->pvalue_field_names->phead; - sllse_t* pb = pvalue_field_values->phead; // for x=1 and y=2 - for ( ; pa != NULL && pb != NULL; pa = pa->pnext, pb = pb->pnext) { - char* value_field_name = pa->value; - char* value_field_sval = pb->value; + int n = pstate->pvalue_field_names->length; + for (int i = 0; i < n; i++) { + char* value_field_name = pstate->pvalue_field_names->strings[i]; + char* value_field_sval = pstate->pvalue_field_values->strings[i]; + if (value_field_sval == NULL) + continue; + int have_dval = FALSE; double value_field_dval = -999.0; diff --git a/c/reg_test/expected/out b/c/reg_test/expected/out index 932f545f4..c0771a763 100644 --- a/c/reg_test/expected/out +++ b/c/reg_test/expected/out @@ -3728,17 +3728,23 @@ a=pan,b=pan,i=1,x=0.3467901443380824,y=0.7268028627434533,x_rsum=0.346790,x_delt a=eks,b=pan,i=2,x=0.7586799647899636,y=0.5221511083334797,x_rsum=1.105470,x_delta=0.411890,x_counter=2,y_rsum=1.248954,y_delta=-0.204652,y_counter=2 aaa=wye,b=wye,i=3,x=0.20460330576630303,y=0.33831852551664776,x_rsum=1.310073,x_delta=-0.554077,x_counter=3,y_rsum=1.587272,y_delta=-0.183833,y_counter=3 a=eks,bbb=wye,i=4,x=0.38139939387114097,y=0.13418874328430463,x_rsum=1.691473,x_delta=0.176796,x_counter=4,y_rsum=1.721461,y_delta=-0.204130,y_counter=4 -a=zee,b=pan,i=6,x=0.5271261600918548,y=0.49322128674835697,x_rsum=2.218599,x_delta=0.145727,x_counter=5,y_rsum=2.214683,y_delta=0.359033,y_counter=5 -a=eks,b=zee,iii=7,x=0.6117840605678454,y=0.1878849191181694,x_rsum=2.830383,x_delta=0.084658,x_counter=6,y_rsum=2.402567,y_delta=-0.305336,y_counter=6 -aaa=hat,bbb=wye,i=9,x=0.03144187646093577,y=0.7495507603507059,x_rsum=2.861825,x_delta=-0.580342,x_counter=7,y_rsum=3.152118,y_delta=0.561666,y_counter=7 -a=pan,b=wye,i=10,x=0.5026260055412137,y=0.9526183602969864,x_rsum=3.364451,x_delta=0.471184,x_counter=8,y_rsum=4.104737,y_delta=0.203068,y_counter=8 +a=wye,b=pan,i=5,xxx=0.5732889198020006,y=0.8636244699032729,y_rsum=2.585086,y_delta=0.729436,y_counter=5 +a=zee,b=pan,i=6,x=0.5271261600918548,y=0.49322128674835697,x_rsum=2.218599,x_delta=0.145727,x_counter=5,y_rsum=3.078307,y_delta=-0.370403,y_counter=6 +a=eks,b=zee,iii=7,x=0.6117840605678454,y=0.1878849191181694,x_rsum=2.830383,x_delta=0.084658,x_counter=6,y_rsum=3.266192,y_delta=-0.305336,y_counter=7 +a=zee,b=wye,i=8,x=0.5985540091064224,yyy=0.976181385699006,x_rsum=3.428937,x_delta=-0.013230,x_counter=7 +aaa=hat,bbb=wye,i=9,x=0.03144187646093577,y=0.7495507603507059,x_rsum=3.460379,x_delta=-0.567112,x_counter=8,y_rsum=4.015743,y_delta=0.561666,y_counter=8 +a=pan,b=wye,i=10,x=0.5026260055412137,y=0.9526183602969864,x_rsum=3.963005,x_delta=0.471184,x_counter=9,y_rsum=4.968361,y_delta=0.203068,y_counter=9 mlr --odkvp step -a rsum,delta,counter -f x,y -g a ./reg_test/input/abixy-het a=pan,b=pan,i=1,x=0.3467901443380824,y=0.7268028627434533,x_rsum=0.346790,x_delta=0.000000,x_counter=1,y_rsum=0.726803,y_delta=0.000000,y_counter=1 a=eks,b=pan,i=2,x=0.7586799647899636,y=0.5221511083334797,x_rsum=0.758680,x_delta=0.000000,x_counter=1,y_rsum=0.522151,y_delta=0.000000,y_counter=1 +aaa=wye,b=wye,i=3,x=0.20460330576630303,y=0.33831852551664776 a=eks,bbb=wye,i=4,x=0.38139939387114097,y=0.13418874328430463,x_rsum=1.140079,x_delta=-0.377281,x_counter=2,y_rsum=0.656340,y_delta=-0.387962,y_counter=2 +a=wye,b=pan,i=5,xxx=0.5732889198020006,y=0.8636244699032729,y_rsum=0.863624,y_delta=0.000000,y_counter=1 a=zee,b=pan,i=6,x=0.5271261600918548,y=0.49322128674835697,x_rsum=0.527126,x_delta=0.000000,x_counter=1,y_rsum=0.493221,y_delta=0.000000,y_counter=1 a=eks,b=zee,iii=7,x=0.6117840605678454,y=0.1878849191181694,x_rsum=1.751863,x_delta=0.230385,x_counter=3,y_rsum=0.844225,y_delta=0.053696,y_counter=3 +a=zee,b=wye,i=8,x=0.5985540091064224,yyy=0.976181385699006,x_rsum=1.125680,x_delta=0.071428,x_counter=2 +aaa=hat,bbb=wye,i=9,x=0.03144187646093577,y=0.7495507603507059 a=pan,b=wye,i=10,x=0.5026260055412137,y=0.9526183602969864,x_rsum=0.849416,x_delta=0.155836,x_counter=2,y_rsum=1.679421,y_delta=0.225815,y_counter=2 mlr --opprint histogram -f x,y --lo 0 --hi 1 --nbins 20 ./reg_test/input/small