Fixed JoinChain bug from Person.cs

This commit is contained in:
MarcUs7i 2024-12-13 23:23:05 +01:00
parent b7367aeb20
commit 0f1420367d

View file

@ -50,15 +50,26 @@ public sealed class Person
/// neighbor of this person; otherwise it will become the right hand side neighbor</param>
public void JoinChain(Person neighbor, bool isLeftNeighbor)
{
// expect that that all neighbors of the new person are null
// so that way we truly know that the provided person is new
if (neighbor.LeftNeighbor == null || neighbor.RightNeighbor == null)
var neighborOfNeighbor = isLeftNeighbor ? neighbor.RightNeighbor : neighbor.LeftNeighbor;
if (neighborOfNeighbor != null)
{
return;
if(isLeftNeighbor)
{
neighborOfNeighbor.LeftNeighbor = this;
RightNeighbor = neighborOfNeighbor;
LeftNeighbor = neighbor;
neighbor.RightNeighbor = this;
}
var thisNeighbor = isLeftNeighbor ? LeftNeighbor : RightNeighbor;
thisNeighbor?.JoinChain(neighbor, isLeftNeighbor);
else
{
neighborOfNeighbor.RightNeighbor = this;
LeftNeighbor = neighborOfNeighbor;
RightNeighbor = neighbor;
neighbor.LeftNeighbor = this;
}
}
else
{
if (isLeftNeighbor)
{
neighbor.RightNeighbor = this;
@ -70,6 +81,7 @@ public sealed class Person
RightNeighbor = neighbor;
}
}
}
/// <summary>
/// Attempts to move the forward bucket along the chain towards the fire.