/** * Unshift, or 'push', a vertex onto the front of the list * * This is confusing, believe me, but I can explain it: * * You start with this - * * v1 v2 v3 v4 (edge->getPrev(), edge->getNext()) * \ / \ / \ / * null -- e1 - e2 - e3 -- null (getNextEdge(), getPreviousEdge()) * * And end with this - * * * v5 v1 v2 v3 v4 (edge->getPrev(), edge->getNext()) * \ / \ / \ / \ / * null -- e5 - e1 - e2 - e3 -- null (getNextEdge(), getPreviousEdge()) * * So a new edge (e5) is created, pointing to the new vertex previously and the old vertex in next * * @param Falcraft\Data\Types\Node\VertexInterface The vertex to make first * * @return int New count of elements (vertices) * */ public function unshiftReference(TypesResource\VertexInterface &$vertex) { // Get the beginning of the list and set it as the current edge $currentEdge =& $this->getFirstEdge(); // $currentEdge = $this->getFirstEdge(); if ($currentEdge->getNextEdge() == null) { /* First edge is last edge is the only edge * * ? ? * \ / * null - CE - null */ if ($currentEdge->getPrevious() == null) { /* null ? * \ / * CE */ // push new value to beginning of list: $currentEdge->setPrevious($vertex); return $this->count(); } if ($currentEdge->getNext() == null) { /* v1 null * \ / * CE */ // push new value to the beginning of list: $currentEdge->setNext($currentEdge->getPrevious()); $currentEdge->setPrevious($vertex); return $this->count(); } } /* v1 v2 new v1 v2 * \ / -> \ /\ / * CE NE CE */ // Construct a new node e5 -> v5 - v1 (v5 is $vertex) $newEdge = new OrderedEdge($vertex, $currentEdge->getPrevious()); /* append edge on to front of list: (null - e1 - e2) becomes (null - e5 - e1 - e2) */ OrderedEdge::connect($newEdge, $currentEdge); // $currentEdge->setPreviousEdge($newEdge); // $newEdge->setNextEdge($currentEdge); // Add the edge to the RestrictedSet. Booyah! $this->edges->addReference($newEdge); return $this->count(); }
echo "Failure...\n"; } echo "Instantiate Ordered Edges -> "; $success = true; $testOrderedEdge1 = $testOrderedEdge2 = null; try { $testOrderedEdge1 = new Types\OrderedEdge($testUnorderedNode1, $testUnorderedNode2); $testOrderedEdge2 = new Types\OrderedEdge($testUnorderedNode3, $testUnorderedNode4); } catch (\Exception $e) { $success = false; } if ($success) { echo "Success!\n"; } else { echo "Failure...\n"; } echo "Linking Ordered Edges -> "; $success = true; try { Types\OrderedEdge::connect($testOrderedEdge1, $testOrderedEdge2); } catch (\Exception $e) { $success = false; } if ($success) { echo "Success!\n\n"; echo "Test Results\n\n"; echo "testOrderedEdge1 Next (Prev Node, Next Node) -> "; echo "(" . $testOrderedEdge1->getNextEdge()->getPrevious()->getIdentifier() . ", " . $testOrderedEdge1->getNextEdge()->getNext()->getIdentifier() . ")\n"; echo "testOrderedEdge2 Previous (Prev Node, Next Node) -> "; echo "(" . $testOrderedEdge2->getPreviousEdge()->getPrevious()->getIdentifier() . ", " . $testOrderedEdge2->getPreviousEdge()->getNext()->getIdentifier() . ")\n"; }