mapping question

Discussion in 'C Programming' started by Angus, Mar 3, 2011.

  1. Angus

    Angus Guest

    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
     
    Angus, Mar 3, 2011
    #1
    1. Advertising

  2. Angus <> 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.*
     
    Anders Wegge Keller, Mar 3, 2011
    #2
    1. Advertising

  3. 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.
     
    luser- -droog, Mar 3, 2011
    #3
  4. Angus

    BartC Guest

    "Angus" <> wrote in message
    news:...
    > 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];

    --
    Bartc
     
    BartC, Mar 3, 2011
    #4
  5. On Mar 3, 9:07 am, 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?


    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.
     
    Michael Angelo Ravera, Mar 3, 2011
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green
    Replies:
    0
    Views:
    4,164
    Roedy Green
    Aug 23, 2003
  2. Troy Makaro

    simple servlet-mapping question

    Troy Makaro, Aug 27, 2003, in forum: Java
    Replies:
    0
    Views:
    329
    Troy Makaro
    Aug 27, 2003
  3. hilz
    Replies:
    9
    Views:
    3,212
    P.Hill
    Jul 27, 2004
  4. Marc Huffnagle

    List mapping question

    Marc Huffnagle, Feb 3, 2005, in forum: Python
    Replies:
    6
    Views:
    331
    Christos TZOTZIOY Georgiou
    Feb 4, 2005
  5. Replies:
    2
    Views:
    385
Loading...

Share This Page