Velocity Reviews > mapping question

# mapping question

Angus
Guest
Posts: n/a

 03-03-2011
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

Anders Wegge Keller
Guest
Posts: n/a

 03-03-2011
Angus <(E-Mail Removed)> writes:

> 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.

--
/Wegge

Leder efter redundant peering af dk.*,linux.debian.*

luser- -droog
Guest
Posts: n/a

 03-03-2011
Angus wrote:
> 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.

BartC
Guest
Posts: n/a

 03-03-2011

"Angus" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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[i][0] && e<=table[i][1]) return e-table[i][0]+offset;
offset+=table[i][1]-table[i][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];

--
Bartc

Michael Angelo Ravera
Guest
Posts: n/a

 03-03-2011
On Mar 3, 9:07*am, Angus <(E-Mail Removed)> wrote:
> 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

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

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.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post kate0104@hotmail.com Cisco 1 04-05-2005 09:32 PM MCScrapE Cisco 0 03-01-2005 08:04 PM ilya@3ka.mipt.ru Cisco 0 01-11-2005 02:42 PM Troy Makaro Java 0 08-27-2003 07:20 PM Roedy Green Java 0 08-23-2003 04:58 AM