Decision tables: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added support for superfluous blanks in answer. -- ~~~~)
(Updated first D entry)
Line 196: Line 196:
<lang d>import std.stdio, std.algorithm, std.exception, std.array;
<lang d>import std.stdio, std.algorithm, std.exception, std.array;


struct DecisionTable {
immutable struct DecisionTable {
alias immutable(bool)[] IBA;
alias immutable(bool)[] IBA;
const string[] conds, actions;
const string[] conds, actions;
Line 204: Line 204:
in size_t newLen)
in size_t newLen)
pure nothrow {
pure nothrow {
bool[] result = new bool[newLen];
auto result = new bool[newLen];
result[0 .. b.length] = b[];
result[0 .. b.length] = b[];
return assumeUnique(result);
return result.assumeUnique;
}
}


this(in string[] c, in string[] a, in bool[][][] q)
this(immutable string[] c,
immutable string[] a,
const pure nothrow {
immutable bool[][][] q) pure nothrow {
conds = c;
conds = c;
actions = a;
actions = a;
Line 217: Line 218:
r[growTo(p[0], conds.length)] =
r[growTo(p[0], conds.length)] =
growTo(p[1], actions.length);
growTo(p[1], actions.length);
rules = assumeUnique(r);
rules = r.assumeUnique;
}
}


Line 226: Line 227:
auto iTested = growTo(tested, conds.length);
auto iTested = growTo(tested, conds.length);
if (iTested in rules)
if (iTested in rules)
foreach (i, immutable(byte) e; rules[iTested])
foreach (immutable i, immutable e; rules[iTested])
if (e) rightActions ~= actions[i];
if (e)
rightActions ~= actions[i];


if (!rightActions.empty)
if (!rightActions.empty)
Line 241: Line 243:
string answer = "no";
string answer = "no";
try
try
answer = stdin.readln();
answer = stdin.readln;
catch (StdioException)
catch (StdioException)
writeln("no");
writeln("no");
Line 253: Line 255:
void main() {
void main() {
enum { F = false, T = true }
enum { F = false, T = true }
immutable d = DecisionTable(
immutable d = immutable(DecisionTable)(
["Printer is unrecognised",
["Printer is unrecognised",
"A red light is flashing",
"A red light is flashing",
Line 274: Line 276:
);
);


d.consult();
d.consult;
}</lang>
}</lang>
{{out}}
{{out}}