HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Calculating optimal video capture card configuration based on channels needed

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
neededoptimalcapturecardvideocalculatingbasedconfigurationchannels

Problem

I have to calculate the configuration of hardware that uses the least number of capture cards based on the number of channels desired. The cards are available in 4, 8, and 16 channel varieties.

It's important to note that the least number of cards is the first priority, and the second priority is using the cheaper (lower channel) cards when possible. Therefore, a 22-channel request could be filled with 2 16-channel cards, however using a 16 and an 8-channel card is more desirable as it uses the same number of physical cards while utilizing a cheaper 8-channel card instead of two 16-channels.

var card_16 = "US-HDVRC2-16-480D";
var card_8 = "US-HDVRC2-8-240D";
var card_4 = "US-HDVRC2-4-120D";

function getCardObject(channels) {
    var cards = {}
    var remainder = channels%16;
    if (channels/16 >= 1) //if there are at least 16 channels
    {
        cards[card_16] = Math.floor(channels/16);
        if (remainder != 0)
        {
            if (remainder > 8)
                cards[card_16] = cards[card_16] + 1;
            else if (remainder > 4)
                cards[card_8] = 1;
            else
                cards[card_4] = 1;
        }
    }
    else if (channels/8 >= 1)  { //if there aren't 16 but at least 8
        if (remainder != 0)
        {
            if (remainder > 8)
                cards[card_16] = 1;
            else if (remainder > 4)
                cards[card_8] = 1;
            else
                cards[card_4] = 1;
        }
    }
    else if (channels/4 >= 1) { //if there are at least 4
        if (remainder != 0)
        {
            if (remainder > 4)
                cards[card_8] = 1;
            else
                cards[card_4] = 1;
        }
    }
    else //if there are less than 4
        cards[card_4] = 1;
    return cards;
};

Solution

Your own answer is pretty good. I would tighten it up a bit. In particular,

  • It's not immediately clear, based on the variable name, what answer represents. Furthermore, since you are only interested in Math.floor(channels/16), I would compute it in a more succinct and efficient way: var sixteens = channels >> 4;.



  • I don't like reassigning cards["US-HDVRC2-16-480D"] or recomputing remainder > 8.



  • To cast a boolean as 1 or 0, you can use the unary + operator.



  • Consider creating the cards object in a single statement. That means you may get some members whose value is 0 — I don't know if that is acceptable to you.



function getCardObject(channels) {
    var sixteens = channels >> 4;   // Math.floor(channels / 16)
    var remainder = channels & 15;  // channels % 16
    return {
        "US-HDVRC2-16-480D": sixteens + (8 < remainder),
        "US-HDVRC2-8-240D": +(4 < remainder && remainder <= 8),
        "US-HDVRC2-4-120D": +(0 < remainder && remainder <= 4),
    };
}


For comparison, here's a single-expression version using explicit formulas for everything. I don't think that it is better than what you have — it's certainly not easy to understand.

function getCardObject(channels) {
    return {
        // Use a 4-channel card if  1 > 4) + ((channels & 15) > 8),
    };
}

Code Snippets

function getCardObject(channels) {
    var sixteens = channels >> 4;   // Math.floor(channels / 16)
    var remainder = channels & 15;  // channels % 16
    return {
        "US-HDVRC2-16-480D": sixteens + (8 < remainder),
        "US-HDVRC2-8-240D": +(4 < remainder && remainder <= 8),
        "US-HDVRC2-4-120D": +(0 < remainder && remainder <= 4),
    };
}
function getCardObject(channels) {
    return {
        // Use a 4-channel card if  1 <= (channels % 16) <= 4
        'US-HDVRC2-4-120D':  +(((channels - 1) & 15) <  4),

        // Use an 8-channel card if  5 <= (channels % 16) <= 8
        'US-HDVRC2-8-240D':  +(((channels - 1) & 12) == 4),

        // 16-channel cards for the rest
        'US-HDVRC2-16-480D': (channels >> 4) + ((channels & 15) > 8),
    };
}

Context

StackExchange Code Review Q#151795, answer score: 2

Revisions (0)

No revisions yet.