Stable marriage problem: Difference between revisions
Content added Content deleted
(Second D version, from the C++ one) |
(Better D alternative version) |
||
Line 540: | Line 540: | ||
Marriages are unstable</pre> |
Marriages are unstable</pre> |
||
===Alternative version=== |
===Alternative version=== |
||
{{trans|C++}} |
|||
<lang d>import std.stdio, std.algorithm, std.array; |
<lang d>import std.stdio, std.algorithm, std.array; |
||
enum F { abi, bea, cath, dee, eve, fay, gay, hope, ivy, jan } |
|||
const menData = [ |
|||
enum M { abe, bob, col, dan, ed, fred, gav, hal, ian, jon } |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
]; |
|||
alias M[][F] PrefMapF; |
|||
const womenData = [ |
|||
alias F[][M] PrefMapM; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
]; |
|||
immutable PrefMapF womenPref; |
|||
alias string[] PrefList; |
|||
immutable PrefMapM menPref; |
|||
alias PrefList[string] PrefMap; |
|||
⚫ | |||
static this() { |
|||
with (F) with (M) { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
menPref = [ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
]; |
|||
⚫ | |||
} |
|||
/// Does 'first' appear before 'second' in preference list? |
/// Does 'first' appear before 'second' in preference list? |
||
bool prefers( |
bool prefers(T)(const(T[]) prefer, const(T) first, const(T) second) pure nothrow |
||
if (is(T == F) || is(T == M)) { |
|||
foreach (p; prefer) { |
foreach (p; prefer) { |
||
if (p == first) return true; |
if (p == first) return true; |
||
Line 582: | Line 592: | ||
} |
} |
||
void checkStability(in Couples engaged, in |
void checkStability(in Couples engaged, in PrefMapM menPref, in PrefMapF womenPref) { |
||
writeln("Stablility:"); |
writeln("Stablility:"); |
||
bool stable = true; |
bool stable = true; |
||
Line 605: | Line 615: | ||
void main() { |
void main() { |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
// init data structures |
|||
foreach (i; 0 .. 10) { // person |
|||
foreach (j; 1 .. 11) { // preference |
|||
menPref[menData[i][0]] ~= menData[i][j]; |
|||
womenPref[womenData[i][0]] ~= womenData[i][j]; |
|||
⚫ | |||
bachelors ~= menData[i][0]; |
|||
⚫ | |||
⚫ | |||
writeln("Matchmaking:"); |
writeln("Matchmaking:"); |
||
Line 650: | Line 649: | ||
writeln("Perturb:"); |
writeln("Perturb:"); |
||
swap(engaged[ |
swap(engaged[F.abi], engaged[F.bea]); |
||
writeln("\tengage abi with ", engaged[ |
writeln("\tengage abi with ", engaged[F.abi], " and bea with ", engaged[F.bea]); |
||
checkStability(engaged, menPref, womenPref); |
checkStability(engaged, menPref, womenPref); |
||
Line 671: | Line 670: | ||
Engagements: |
Engagements: |
||
abi and jon |
abi and jon |
||
⚫ | |||
ivy and abe |
ivy and abe |
||
eve and hal |
eve and hal |
||
jan and ed |
|||
⚫ | |||
bea and fred |
bea and fred |
||
fay and dan |
fay and dan |
||
cath and bob |
|||
gay and gav |
gay and gav |
||
⚫ | |||
⚫ | |||
Stablility: |
Stablility: |
||
(all marriages stable) |
(all marriages stable) |