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 { |
||
auto result = new bool[newLen]; |
|||
result[0 .. b.length] = b[]; |
result[0 .. b.length] = b[]; |
||
return |
return result.assumeUnique; |
||
} |
} |
||
this( |
this(immutable string[] c, |
||
immutable string[] a, |
|||
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 = |
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 |
foreach (immutable i, immutable e; rules[iTested]) |
||
if (e) |
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}} |