Associative arrays in C

前端 未结 7 1313
感动是毒
感动是毒 2020-12-01 00:33

I am implementing a way to transfer a set of data to a programmable dongle. The dongle is based on a smart card technology and can execute an arbitrary code inside. The inpu

7条回答
  •  离开以前
    2020-12-01 01:24

    Mark Wilkins gave you the right answer. If you want to send the data as a single chunk, you need to understand how C++ maps are represented in your architecture and write the access functions.

    Anyway, if you decide to recreate the map on the dongle, I've written a small C library where you could write thinks like:

    tbl_t in_data=NULL;
    
    tblSetSS(in_data,"method","calc_r");
    tblSetSN(in_data,"id",12);
    tblSetSF(in_data,"loc_a",56.19);
    tblSetSF(in_data,"loc_l",44.02);
    

    and then:

    char  *method_name = tblGetP(in_data, "method");
    int    id          = tblGetN(in_data, "id");
    float  loc_a       = tblGetF(in_data, "loc_a");
    float  loc_l       = tblGetF(in_data, "loc_l");
    

    The hashtable is a variation of the Hopscotch hash, which is rather good on average, and you can have any mix of type for keys and data (i.e. you can use an entire table as a key).

    The focus for that functions was on easing programming rather than pure speed and the code is not thoroughly tested but if you like the idea and want to expand on it, you can have a look at the code on googlecode.

    (There are other things like variable length strings and a fast sttring pattern matching function but those might not be of interest in this case).

提交回复
热议问题