I would treat your four logical elements as four bits, since the integer is from 0 to 15. I would create an array with 16 elements and store a pointer to a function in each element of the array. Every time you need to do this, I would evaluate the boolean values ββin the bitmap, convert to int and call the method stored in this array index.
I know what you're asking about PHP, which I'm afraid I don't know. In C #, you can do something like this:
static class Multiplexer
{
public static string Multiplex(bool a, bool b, bool c, bool d)
{
var i = 0;
i |= (a ? 1 : 0) << 3;
i |= (b ? 1 : 0) << 2;
i |= (c ? 1 : 0) << 1;
i |= (d ? 1 : 0);
return _functions[i]();
}
private static Func<string>[] _functions = new Func<string>[] {
() => { return "pie";},
() => { return "index 1"; },
() => { return DateTime.Now.ToString(); },
() => { return "pie";},
() => { return "index 1"; },
() => { return DateTime.Now.ToString(); },
() => { return Assembly.GetExecutingAssembly().FullName; },
() => { return ""; },
() => { return "pie";},
() => { return "index 1"; },
() => { return DateTime.Now.ToString(); },
() => { return "pie";},
() => { return "index 1"; },
() => { return DateTime.Now.ToString(); },
() => { return Assembly.GetExecutingAssembly().FullName; },
() => { return ""; }};
}
source
share