-
-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy pathPerson.cs
39 lines (34 loc) · 1.44 KB
/
Person.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// submitted by Julian Schacher (jspp) with great help by gustorn and Marius Becker
using System.Collections.Generic;
namespace StableMarriageProblem
{
public class Person<TSelf, TPref>
where TSelf : Person<TSelf, TPref>
where TPref : Person<TPref, TSelf>
{
public string Name { get; set; }
public TPref Partner { get; set; }
public IList<TPref> Choices { get; set; }
// CurrentTopChoice equals the Choice in Choices that is the TopChoice,
// if already tried women are not counted.
public int CurrentTopChoiceIndex { get; set; } = 0;
public Person(string name) => Name = name;
public void ProposeToNext()
{
var interest = GetNextTopChoice();
// If the interest has no partner or prefers this person,
// change interest's partner to this person.
if (interest.Partner == null ||
interest.Choices.IndexOf(this as TSelf) < interest.Choices.IndexOf(interest.Partner))
{
// Should the interest already have a partner, set the partner
// of the interest's partner to null.
if (interest.Partner != null)
interest.Partner.Partner = null;
interest.Partner = this as TSelf;
Partner = interest;
}
}
private TPref GetNextTopChoice() => Choices[CurrentTopChoiceIndex++];
}
}