示例#1
0
 /**
  * Accepts the specified visitor and makes it visit all the objects
  * in this deap.
  *
  * @param object IVisitor $visitor The visitor to accept.
  */
 public function accept(IVisitor $visitor)
 {
     for ($i = 2; $i < $this->count + 2; ++$i) {
         if ($visitor->isDone()) {
             return;
         }
         $visitor->visit($this->array[$i]);
     }
 }
示例#2
0
 /**
  * Causes a visitor to visit the vertices of this graph
  * in topological order.
  * This method takes a visitor and,
  * as long as the IsDone method of that visitor returns false,
  * this method invokes the Visit method of the visitor
  * for each vertex in the graph.
  * The order in which the vertices are visited
  * is given by a topological sort of the vertices.
  *
  * @param object IVisitor $visitor The visitor to accept.
  */
 public function topologicalOrderTraversal(IVisitor $visitor)
 {
     $inDegree = new BasicArray($this->numberOfVertices);
     for ($v = 0; $v < $this->numberOfVertices; ++$v) {
         $inDegree[$v] = 0;
     }
     foreach ($this->getEdges() as $edge) {
         $to = $edge->getV1();
         $inDegree[$to->getNumber()] += 1;
     }
     $queue = new QueueAsLinkedList();
     for ($v = 0; $v < $this->numberOfVertices; ++$v) {
         if ($inDegree[$v] == 0) {
             $queue->enqueue($this->vertex[$v]);
         }
     }
     while (!$queue->isEmpty() && !$visitor->isDone()) {
         $v = $queue->dequeue();
         $visitor->visit($v);
         foreach ($v->getSuccessors() as $to) {
             $inDegree[$to->getNumber()] -= 1;
             if ($inDegree[$to->getNumber()] == 0) {
                 $queue->enqueue($to);
             }
         }
     }
 }
示例#3
0
 /**
  * Accepts a visitor and makes it visit the elements of this partition.
  *
  * @param object IVisitor $visitor The visitor to accept.
  */
 public function accept(IVisitor $visitor)
 {
     for ($item = 0; $item < $this->universeSize; ++$item) {
         if ($visitor->isDone()) {
             return;
         }
         $visitor->visit($this->array[$i]);
     }
 }
示例#4
0
 /**
  * Calls the visit method of the given visitor
  * for each object in this container.
  *
  * @param object IVisitor $visitor A visitor.
  */
 public function accept(IVisitor $visitor)
 {
     foreach ($this as $obj) {
         if ($visitor->isDone()) {
             break;
         }
         $visitor->visit($obj);
     }
 }
示例#5
0
 /**
  * Causes a visitor to visit the nodes of this tree
  * in breadth-first traversal order starting from this node.
  * This method invokes the visit method of the visitor
  * for each node in this tree.
  * The default implementation is iterative and uses a queue
  * to keep track of the nodes to be visited.
  * The traversal continues as long as the isDone
  * method of the visitor returns false.
  *
  * @param object IVisitor $visitor The visitor to accept.
  */
 public function breadthFirstTraversal(IVisitor $visitor)
 {
     $queue = new QueueAsLinkedList();
     if (!$this->isEmpty()) {
         $queue->enqueue($this);
     }
     while (!$queue->isEmpty() && !$visitor->isDone()) {
         $head = $queue->dequeue();
         $visitor->visit($head->getKey());
         for ($i = 0; $i < $head->getDegree(); ++$i) {
             $child = $head->getSubtree($i);
             if (!$child->isEmpty()) {
                 $queue->enqueue($child);
             }
         }
     }
 }