Created
September 12, 2024 10:17
-
-
Save chadyred/0c9974661ba0ba8eb21bb1ed19d8c72a to your computer and use it in GitHub Desktop.
None linear node printer : link could come from anywhere
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Edge | |
{ | |
public function __construct(public readonly Verticle $linkedFrom, public readonly Verticle $linkedTo) | |
{ | |
} | |
} | |
class Verticle | |
{ | |
public function __construct(public readonly int $version) | |
{ | |
} | |
public function equals(Verticle $that) | |
{ | |
return $this === $that; | |
} | |
} | |
class NodeMapper | |
{ | |
/** | |
* @param array<Edge> $edges | |
*/ | |
public function __construct(public readonly array $edges) | |
{ | |
} | |
public function printEdgeRelationMapping() | |
{ | |
$previousTo = null; | |
foreach ($this->edges as $key => $edge) { | |
$from = $edge->linkedFrom; | |
$to = $edge->linkedTo; | |
if ($previousTo?->equals($from)) { | |
echo $to->version; // From shoul not be dipslay because it is the same as before | |
} else { | |
echo $from->version . ' -> ' . $to->version; | |
} | |
if (array_key_last($this->edges) !== $key) { | |
echo ' -> '; | |
} | |
$previousTo = $to; | |
} | |
} | |
} | |
$v1 = new Verticle(1); | |
$v2 = new Verticle(2); | |
$v3 = new Verticle(3); | |
$v4 = new Verticle(4); | |
$v5 = new Verticle(5); | |
# v1 -> v2 -> v4 -> V3 -> v5 -> v1 | |
$e1 = new Edge($v1, $v2); | |
$e2 = new Edge($v2, $v4); | |
$e3 = new Edge($v4, $v3); | |
$e4 = new Edge($v5, $v1); | |
$nodeMapper = new NodeMapper([$e1, | |
$e2, | |
$e3, | |
$e4 | |
]); | |
$nodeMapper->printEdgeRelationMapping(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment