Pfitz
Pfitz

Reputation: 7344

Is there are more elegant Solution for a large switch statement?

I have got map a lot of ranges to a value like 0-300 = 10 , 300-600 = 20, 600-900 = 30 ... 2500000-2700000 = 7000 .... So I could make a really large switch-statement/if-block but I wonder if there is a more elegant approach to solve this little problem.

Ok here is a small subset of the table with real data:

0-300 : 25
301-600.  : 45
601-900 : 65
901-1200. : 85
1201-1500: 105

1501-2000 : 133
2001-2500 : 161
2501-3000: 189
3001-3500:217
3501-4000:245

4001-4500:273
4501-5000:301
5001-6000:338

Upvotes: 1

Views: 732

Answers (3)

yuji
yuji

Reputation: 16725

The most common pattern for getting rid of a switch statement is to use a dictionary. In your case, since you're mapping ranges, you'll use an NSArray of range cutoffs instead. This is what it would look like if you're dealing with ints:

NSArray *rangeCutoffs = [NSArray arrayWithObjects:[NSNumber numberWithInt:300],[NSNumberWithInt:600],...,nil];
NSArray *values = [NSArray arrayWithObjects:[NSNumber numberWithInt:10], [NSNumber numberWithInt:20],...,nil];

int mappedInt;
for (int index=0; index <= [rangeCutoffs count]; index++) {
    if (intToMap < [[rangeCutoffs objectAtIndex:index] intValue]) {
        mappedInt = [[values objectAtIndex:index] intValue];
    }
}
if (mappedInt == 0) {
    mappedInt = [[values lastObject] intValue];
}

In practice you'd want to load rangeCutoffs and values from a plist instead of hardcoding them.

Upvotes: 5

jweyrich
jweyrich

Reputation: 32260

You could do something like this (C example):

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

typedef int range_type;
typedef int value_type;

typedef struct {
    range_type min;
    range_type max;
    value_type value;
} range_t;

const range_t *find_range(const range_t *ranges, size_t rangesSize,
    value_type valueToFind)
{
    for (size_t i = 0; i < rangesSize; ++i) {
        if (ranges[i].min <= valueToFind && valueToFind <= ranges[i].max)
            return &ranges[i];
    }
    return NULL;
}

int main() {
    const range_t ranges[] = {
        {   0,  300, 10 },
        { 301,  600, 20 },
        { 601,  900, 30 },
        { 901, 1200, 40 }
        // And so on...
    };

    value_type testValues[] = {
          -1,                   // None
           0, 299,  300,    // [  0,  300]
         301, 599,  600,    // [301,  600]
         601, 899,  900,    // [601,  900]
         901, 1199, 1200,   // [901, 1200]
         // And so on...
    };

    for (size_t i = 0; i < sizeof(testValues) / sizeof(testValues[0]); ++i) {
        const range_t *match = find_range(
            ranges, sizeof(ranges) / sizeof(ranges[0]), testValues[i]);
        if (match != NULL)
            printf("%d found at [%d..%d]\n", testValues[i], match->min,
                match->max);
        else
            printf("%d not found\n", testValues[i]);
    }
    return EXIT_SUCCESS;
}

Should output:

-1 not found
0 found at [0..300]
299 found at [0..300]
300 found at [0..300]
301 found at [301..600]
599 found at [301..600]
600 found at [301..600]
601 found at [601..900]
899 found at [601..900]
900 found at [601..900]
901 found at [901..1200]
1199 found at [901..1200]
1200 found at [901..1200]

Upvotes: 0

JeremyP
JeremyP

Reputation: 86651

You could use a table. e.g.

struct Lookup
{
    int min;
    int max;
    int value;
};

struct Lookup table[] =
{
    {       0,     300,   10 },
    {     301,     600,   20 },
    {     601,     900,   30 },
    // other ranges
    { 2500000, 2700000, 7000 },
    { -1, -1, -1 } // marks the end of the table
};

And then simply iterate through it to find the right range

int result = -1;
for (int i = 0 ; table[i].min != -1 && result == -1 ; ++i)
{
     if (table[i].min <= value && value <= table[i].max)
     {
         result = table[i].value;
     }
}

If it's a really large table, you can use a binary search instead.

Upvotes: 1

Related Questions