I want to map struct members so I can eliminate if branches in a loop. What is the best way or convention to implement this in C? I suppose it could be a 2 dimensional array instead...then I could map integers to the char keys?
char chunk[32];
int n;
int i;
char *ptr = config;
while (*ptr != '\0') {
int items_read = sscanf(ptr, "%31[^;]%n", chunk, &n);
if(chunk[0] == 'S' && chunk[1] == 'P') {
for(i=0;i<GLOBAL_MEAS_CUTOFF; i++) {
theMeas[i].signal_path = atoi(&chunk[2]);
}
}
if(chunk[0] == 'T' && chunk[1] == 'L') {
for(i=0;i<GLOBAL_MEAS_CUTOFF; i++) {
theMeas[i].trace_length = atoi(&chunk[2]);
}
}
if(chunk[0] == 'S' && chunk[1] == 'R') {
for(i=0;i<GLOBAL_MEAS_CUTOFF; i++) {
theMeas[i].sample_rate = atoi(&chunk[2]);
}
}
chunk[0]='\0';
if (items_read == 1)
ptr += n;
if ( *ptr != ';' ) {
break;
}
++ptr;
}
I suspect what you (ideally) want is a dictionary:
Of course, the above syntax will never happen in C, but that's not really important here. The problem is that you would have to write all the code implementing a dictionary data type, and I suspect that's overkill.
So I suspect what you (really) want is a way to have names that can be used in a loop:
And I'm here to tell you that you can do this (kind of)! The simplest way is with an
enum
:Instead of
struct
members, you use an array:To index a "member", use this:
You can loop through all the "members," you can use this:
This does break down a little when you want to get character-based comparisons, but we can do a little bit:
Your
if
statements could be replaced with:Or, more safely:
Which is about as close as I can get.
Note that I've deliberately used a naming scheme to facilitate the creation of macros that will automate much of this. Let's try:
Usage:
That last bit doesn't seem so bad. It still allows you something close to
struct
-like access viatheMeas[i][signal_path]
, but allows you to iterate over the "members," and hides most of the heavy lifting behind macros.The
to_str
andfrom_str
functions take a little more macro trickery to automate. You'll probably need to look into P99 for that. Thefrom_abv
function isn't something I'd recommend for the general case, as we have no way of guaranteeing that the next time you make iterable fields you'll use names with underscores. (Of course, you could drop thefrom_abv
function and give your members inscrutable names likeSP
,TL
, andSR
, allowing you to directly compare them to your string data, but you'd need to change thestrcmp
to amemcmp
with a size argument of(sizeof(#x) - 1)
. Then all the places you havefrom_abv
you'd just usefrom_str
, which can be automatically generated for you.)However,
from_abv
isn't hard to define, and you could honestly just copy and paste yourif
blocks from above into it - it'd be slightly more efficient, though if you added a "member" you'd have to update the function (as written, it'll update itself if you add a member.)Unfortunately with simple C99 this is not possible, since array indices can only be unsigned integers. But propably the
strncmp()
function suits you more?C supports pointers to functions, so you could create an array of pointers to functions and address the array according to your input. This would require you to implement additional functions with same signatures.
Another way might be to encapsulate the if-clauses in a separate function and calling it with the arguments.
However, I think neither way you will gain much speedup, if any.
If (and it's a fairly big if) you can rely on the data always being one of those three options, then we could construct a "minimal perfect hash" over the three cases. Assuming the charset is ASCII (or consistent with ASCII):
So, S+P is 3 mod 4, T+L is 0 mod 4, and S+R is 1 mod 4. Not minimal, but close enough:
You might prefer to put some lipstick on this pig with macros or inline functions, or instead of
int *fieldptr
have achar *fieldptr
, start it at((char*)theMeas) + offset
, and increment it bysizeof(Mea)
each time.If you can't rely on friendly data, then you need at least one branch of some kind (a conditional or a call through a function pointer), just to avoid writing anything in the case where the data is bad. Even to keep it to 1 you probably need a 64k-entry lookup table for 3 cases, which is kind of sparse, so you're probably better off with the conditionals.
You might rewrite your logic something like this, using a pointer to an integer:
This approach separates the decision about what variable to change (the
if
statements) from the code that actually does the work that is common to each case (thefor
loop).You will of course want to check whether
p == NULL
in casechunk[0]
andchunk[1]
didn't match anything you were expecting.