public static Link[] GetLeftElements(Link startLink, Link leftLink) { var result = new Link[4]; TryStepLeft(startLink, leftLink, result, 0); startLink.WalkThroughReferersBySource(couple => { if (couple.Linker == Net.And) if (TryStepLeft(couple, leftLink, result, 2)) return Link.Stop; return Link.Continue; }); return result; }
private static void RegisterExistingLetter(Link letter) { letter.WalkThroughReferersBySource(referer => { if (referer.Linker == Net.Has) { Link target = referer.Target; if (target.Source == Net.Code && target.Linker == Net.ThatIsRepresentedBy) { char charCode = (char)LinkConverter.ToNumber(target.Target); CharactersToLinks[charCode] = letter; LinksToCharacters[letter] = charCode; } } }); }
public static bool TryStepRight(Link startLink, Link rightLink, Link[] result, int offset) { int added = 0; startLink.WalkThroughReferersBySource(couple => { if (couple.Linker == Net.And) { var coupleTarget = couple.Target; if (coupleTarget == rightLink) { result[offset] = couple; if (++added == 2) return Link.Stop; } else if (coupleTarget.Linker == Net.And && coupleTarget.Source == rightLink) { result[offset + 1] = couple; if (++added == 2) return Link.Stop; } } return Link.Continue; }); return added > 0; }
private static Link TryReconstructConnection(Link first, Link second) { Link directConnection = null; if (second.ReferersBySourceCount < first.ReferersBySourceCount) { // o_| x_o ... // x_| |___| // // <- second.WalkThroughReferersBySource(couple => { if (couple.Linker == Net.And && couple.ReferersByTargetCount == 1 && couple.ReferersBySourceCount == 0) { Link neighbour = couple.FirstRefererByTarget; if (neighbour.Linker == Net.And && neighbour.Source == first) { if (directConnection == null) directConnection = first & second; Link.Update(ref neighbour, directConnection, Net.And, couple.Target); //Link.Delete(ref couple); // Можно заменить удалением couple } } if (couple.Linker == Net.And) { Link neighbour = couple.FirstRefererByTarget; if (neighbour.Linker == Net.And && neighbour.Source == first) { } } }); } else { // o_| x_o ... // x_| |___| // // -> first.WalkThroughReferersBySource(couple => { if (couple.Linker == Net.And) { Link neighbour = couple.Target; if (neighbour.Linker == Net.And && neighbour.Source == second) { if (neighbour.ReferersByTargetCount == 1 && neighbour.ReferersBySourceCount == 0) { if (directConnection == null) directConnection = first & second; Link.Update(ref couple, directConnection, Net.And, neighbour.Target); //Link.Delete(ref neighbour); } } } }); } if (second.ReferersByTargetCount < first.ReferersByTargetCount) { // |_x ... x_o // |_o |___| // // <- second.WalkThroughReferersByTarget(couple => { if (couple.Linker == Net.And) { Link neighbour = couple.Source; if (neighbour.Linker == Net.And && neighbour.Target == first) { if (neighbour.ReferersByTargetCount == 0 && neighbour.ReferersBySourceCount == 1) { if (directConnection == null) directConnection = first & second; Link.Update(ref couple, neighbour.Source, Net.And, directConnection); //Link.Delete(ref neighbour); } } } }); } else { // |_x ... x_o // |_o |___| // // -> first.WalkThroughReferersByTarget((couple) => { if (couple.Linker == Net.And && couple.ReferersByTargetCount == 0 && couple.ReferersBySourceCount == 1) { Link neighbour = couple.FirstRefererBySource; if (neighbour.Linker == Net.And && neighbour.Target == second) { if (directConnection == null) directConnection = first & second; Link.Update(ref neighbour, couple.Source, Net.And, directConnection); Link.Delete(ref couple); } } }); } if (directConnection != null) { CompressionsCount++; } return directConnection; }