mapping question

A

Angus

Hello

I have error codes in 10 integer ranges. Eg 1000-1020, 1200-1215,
2000-2010, etc

ie there is no particular pattern. I need to map these errors onto a
continuous range of different error integral values.

I will create a function whereby the caller passes the error code and
function returns the (different) continous error codes. I obvously
will have a mapping between the two.

I want to make this as efficient as possible. What is the most
efficient way to do this?

Angus
 
A

Anders Wegge Keller

I want to make this as efficient as possible. What is the most
efficient way to do this?

Why do you need to optimize this particular operation? Error handling
would be the last part of my code I would target for "most efficient
way".

But that aise, use bisection, either in an array of (min, max,
mapped_error), or if you want to obfuscate it at the same time, by a
huge tree of nested if statements, starting with the middle part of
the range.
 
L

luser- -droog

Angus said:
Hello

I have error codes in 10 integer ranges. Eg 1000-1020, 1200-1215,
2000-2010, etc

ie there is no particular pattern. I need to map these errors onto a
continuous range of different error integral values.

I will create a function whereby the caller passes the error code and
function returns the (different) continous error codes. I obvously
will have a mapping between the two.

I want to make this as efficient as possible. What is the most
efficient way to do this?

Use perl to generate a big switch statement!
Switches is fast.
 
B

BartC

Angus said:
Hello

I have error codes in 10 integer ranges. Eg 1000-1020, 1200-1215,
2000-2010, etc

ie there is no particular pattern. I need to map these errors onto a
continuous range of different error integral values.

I will create a function whereby the caller passes the error code and
function returns the (different) continous error codes. I obvously
will have a mapping between the two.

I want to make this as efficient as possible. What is the most
efficient way to do this?

Why do the error values need to be continuous? Depending on the answer, an
easy solution is to just add dummy ranges of error codes: 0-999, 1021-1199,
1216-1999 and so on, to fill the gaps. Then the function just returns it's
argument unchanged.

Otherwise the simplest code is the most obvious:

#include <stdio.h>
#include <stdlib.h>

/* map e into compact range 1..N
return 0 when e does not correspond to an error code
*/

int map_errorcode(int e){
#define nranges 3
int table[nranges][2]={
{1000,1020},
{1200,1215},
{2000,2010}};
int i,offset;

offset=1;
for (i=0; i<nranges; ++i){
if (e>=table[0] && e<=table[1]) return e-table[0]+offset;
offset+=table[1]-table[0]+1;
}
return 0;
}

int main(void){
int i,a;

for (i=0; i<3000; ++i) {
a = map_errorcode(i);
if (a) printf("%d => %d\n",i,a);
}
}

This is not the fastest way, but is fairly compact. If speed is important
for an error-routine, then create a table of 0..3000 entries (depending on
range of error codes), and initialise the table to map the errorcode to the
compact code. Then it can be used as follows;

a = table[e];
 
M

Michael Angelo Ravera

Hello

I have error codes in 10 integer ranges.  Eg 1000-1020, 1200-1215,
2000-2010, etc

ie there is no particular pattern.  I need to map these errors onto a
continuous range of different error integral values.

I will create a function whereby the caller passes the error code and
function returns the (different) continous error codes.  I obvously
will have a mapping between the two.

I want to make this as efficient as possible.  What is the most
efficient way to do this?

I concur with others who have said that optimizing this part of your
code would be a low prioroty, but that having been said, you have
major conflicting goals in optiization. One is code space and the
other is execution speed.


If you want execution speed, you can bounds check the argument for
being inside the "possible" range and create a huge table that
includes all of the intermediate values and return "It's not a legal
value" for those.

In the specific problem, you could create 7 distinct ranges
partitioned by

static int range_list [7] = {999, 1020, 1199, 1215, 1999, 2010,
MAXINT};

When you determine which range you are in, you then determine what
value to subtract from the value supplied and put those nubers in a
table (using -1856 as a special case)

#define OOR_err 48
#define bad_err -1856
static int c [7] = {bad_err, 1000, bad_err, 1179, bad_err, 1963,
bad_err};

int cont_err (int errnum)
{
int idx;
for (idx = 0; errnum <= range_list [idx]; idx ++)
;

if (range_list [idx] == bad_err)
return OOR_err;

return errnum - range_offset [idx];
}

If this were a much larger set of discontinuous ranges, you ight be
able to speed things up with a binary search of the ranges, but I am
fairly sure that thsi won't help much in the case of just 7.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top