1

Just wondering whether there is there a cleaner method of doing this. The user will call setFullScaleRange1 and setFullScaleRange2. The range value they will set is just in integer form. This then goes into a switch statement and gets converted into a hex value. I don’t want the user having to send in a hex value so this is why I have this roundabout way of doing it. Is there a more efficient way of doing this?

void label::setFullScaleRange1(uint16_t range)
{
    uint8_t data = 0;
    uint8_t user_range = 0;
    user_range = get_user_range(range, true);
    data = SET_SLICE(data, REG_TEST1, user_range);
    spi_reg_write(data, REG_CONFIG);
}

void label::setFullScaleRange2(uint16_t range)
{
    uint8_t data = 0;
    uint8_t user_range = 0;
    user_range = get_user_range(range, false);
    data = SET_SLICE(data, REG_TEST2, user_range);
    spi__write(data, REG_CONFIG);
}


static uint8_t get_user_range(uint16_t range, bool test_range)
{   
    if(test_range) {
        switch(range) {
        case 125: 
            range = SET_125_DEG_SEC;
            break;
        case 250: 
            range = SET_250_DEG_SEC;
            break;
        case 500: 
            range = SET_500_DEG_SEC;
            break;
        case 1000:
            range = SET_1000_DEG_SEC;
            break;
        case 2000:
            range = SET_2000_DEG_SEC;
            break;
        default:
            return 1;
        }
    } else {
        switch(range) {
        case 2: 
            range = SET_2G;
            break;
        case 4: 
            range = SET_4G;
            break;
        case 8: 
            range = SET_8G;
            break;
        case 16:
            range = SET_16G;
            break;
        default:
            return 1;
        }
    }
    return range;
}
7
  • 3
    Make a dict, checkout the dict?
    – luoluo
    Sep 18, 2015 at 10:37
  • I second @luolu. That's often the case: You can mutually substitute code complexity and data complexity. The right balance isn't always clear-cut (towards data complexity). As much as we abhor the code duplication shown (among other things, is just no fun!), it is totally obvious and clear-cut and trivial. That's sometimes an asset. Sep 18, 2015 at 10:45
  • I'm also curious what e.g. SET_16G is defined to. It's not, by any remote chance, 0x10000? Sep 18, 2015 at 10:48
  • 3
    @luoluo Did you mean a std::map actually? Sep 18, 2015 at 10:49
  • 5
    If there is a computable relationship between the user input and the resulting range it would be preferable to compute it. But you leave us in the dark ;-). Sep 18, 2015 at 10:52

3 Answers 3

4

In C++, you can give get_user_range an array of static maps that store the various constants to return

#include <cstdint>
#include <iostream>
#include <map>

enum E1 { SET_125_DEG_SEC = 125, SET_250_DEG_SEC = 250, SET_500_DEG_SEC = 500, SET_1000_DEG_SEC = 1000, SET_2000_DEG_SEC = 2000 };
enum E2 { SEG_2G = 2, SEG_4G = 4, SEG_8G = 8, SEG_16G = 16 };

static int get_user_range(uint16_t range, bool test_range)
{   
    static std::map<uint16_t, int> switch_map[2] = 
    {
        { 
            {  125, SET_125_DEG_SEC },
            {  250, SET_250_DEG_SEC },
            {  500, SET_500_DEG_SEC },
            { 1000, SET_1000_DEG_SEC },
            { 2000, SET_2000_DEG_SEC }
        },
        { 
            {  2, SEG_2G },
            {  4, SEG_4G },
            {  8, SEG_8G },
            { 16, SEG_16G }
        }
    };

    auto const& m = switch_map[test_range];
    auto it = m.find(range);
    if (it != m.end()) {
        return it->second;
    }

    return 1;
}

int main()
{
    std::cout << get_user_range(500, false) << '\n';
    std::cout << get_user_range( 16, false) << '\n';
    std::cout << get_user_range(500, true) << '\n';
    std::cout << get_user_range( 16, true) << '\n';
}

Live Example.

What this code does is to index the map-array with the boolean test_range. Then given the correct map, it will call its find member function with the integer range. If this function returns an iterator (basically a pointer if you are coming from C) that is equal to one past the end of the map, it hasn't found what you were looking for. If that's the case, you return 1. Otherwise, you dereference the iterator, which gives a std::pair<uint16_t, int> struct, and you take the .second data member of it which contains the value you want.

You can easily translate this to a C-style array in which you do something similar. But you'd have to work a little harder to search for the data.

Note that I changed the return type to int in order to be able to give natural values to the various enum constants.

0

Yes , make an array of pointers to functions do some algorithm to change the range of the numbers from big number to single digit , there are some ways to do that . (enum optional!) and your algorithm will be much shorter, without any switch or manny if statements.

0

In case there is some sort of mathematical relation between the input and the output values, you could perhaps calculate the output value, which would be the most efficient by far.

Otherwise, you will have to store all inputs in a sorted array, and then all outputs in another sorted array. Binary search the input array for a particular value, grab the array index where you found it, then take the corresponding value from the output table.

Alternatively, you can make a struct/class containing one input value and one output value, then make an array of those structs/classes.

  • In C, you can use the bsearch() function.
  • In C++, you can use a container such as std::set or std::map and search with std::find.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.