Beispiel #1
0
 /**
  * @return boolean
  */
 public function isBinary()
 {
     if ($this->_frames->isEmpty()) {
         throw new \UnderflowException('Not enough data has been received to determine if message is binary');
     }
     return Frame::OP_BINARY === $this->_frames->bottom()->getOpcode();
 }
 /**
  * Retrieve the maintenance task entry, from unique identfier
  * @param $maintenanceTaskEntryIdentifier   Identifier to locate entry
  * @return mixed|null                       Returns the maintenance task entry or null on error
  * @throws InvalidArgumentException         if the provided argument is not set or of correct type
  */
 public function retrieveMaintenanceTaskEntry($maintenanceTaskEntryIdentifier)
 {
     if (!isset($maintenanceTaskEntryIdentifier)) {
         //argument check
         throw new InvalidArgumentException("Missing Argument");
     } else {
         if (!is_numeric($maintenanceTaskEntryIdentifier)) {
             //argument check
             throw new InvalidArgumentException("maintenanceTaskEntryIdentifier is not a number");
         }
     }
     if ($this->maintenanceTaskList->isEmpty()) {
         //if list is empty, unable to return entry
         return null;
     } else {
         $this->maintenanceTaskList->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO);
         //set iteration FIFO
         for ($this->maintenanceTaskList->rewind(); $this->maintenanceTaskList->valid(); $this->maintenanceTaskList->next()) {
             if ($this->maintenanceTaskList->current()->getTaskEntryIdentifier() == $maintenanceTaskEntryIdentifier) {
                 //if entry identifier matches supplied identifier
                 return $this->maintenanceTaskList->current();
             }
             //return the matching entry
         }
     }
     return null;
     //entry with given identifier not found
 }
Beispiel #3
0
 /**
  * Perform a depth-first traversal on the provided graph.
  *
  * @param Digraph $graph
  *   The graph on which to perform the depth-first search.
  * @param DepthFirstVisitorInterface $visitor
  *   The visitor object to use during the traversal.
  * @param object|\SplDoublyLinkedList $start
  *   A vertex, or vertices, to use as start points for the traversal. There
  *   are a few sub-behaviors here:
  *     - If an SplDoublyLinkedList, SplQueue, or SplStack is provided, the
  *       traversal will deque and visit vertices contained therein.
  *     - If a single vertex object is provided, it will be the sole
  *       originating point for the traversal.
  *     - If no value is provided, DepthFirst::find_sources() is called to
  *       search the graph for source vertices. These are place into an
  *       SplQueue in the order in which they are discovered, and traversal
  *       is then run over that queue in the same manner as if calling code
  *       had provided a queue directly. This method *guarantees* that all
  *       vertices in the graph will be visited.
  *
  * @throws RuntimeException
  *   Thrown if an invalid $start parameter is provided.
  */
 public static function traverse(Digraph $graph, DepthFirstVisitorInterface $visitor, $start = NULL)
 {
     if ($start === NULL) {
         $queue = self::find_sources($graph, $visitor);
     } else {
         if ($start instanceof \SplDoublyLinkedList) {
             $queue = $start;
         } else {
             if (is_object($start)) {
                 $queue = new \SplDoublyLinkedList();
                 $queue->push($start);
             }
         }
     }
     if ($queue->isEmpty()) {
         throw new RuntimeException('No start vertex or vertices were provided, and no source vertices could be found in the provided graph.', E_WARNING);
     }
     $visiting = new \SplObjectStorage();
     $visited = new \SplObjectStorage();
     $visitor->beginTraversal();
     $visit = function ($vertex) use($graph, $visitor, &$visit, $visiting, $visited) {
         if ($visiting->contains($vertex)) {
             $visitor->onBackEdge($vertex, $visit);
         } else {
             if (!$visited->contains($vertex)) {
                 $visiting->attach($vertex);
                 $visitor->onStartVertex($vertex, $visit);
                 foreach ($graph->successorsOf($vertex) as $head) {
                     $visitor->onExamineEdge($vertex, $head, $visit);
                     $visit($head);
                 }
                 $visitor->onFinishVertex($vertex, $visit);
                 $visiting->detach($vertex);
                 $visited->attach($vertex);
             }
         }
     };
     // TODO experiment with adding a generator-producing visitor method that yields the queue here
     while (!$queue->isEmpty()) {
         $vertex = $queue->shift();
         $visit($vertex);
     }
     $visitor->endTraversal();
 }
 /**
  * Perform a depth-first traversal on the provided graph.
  *
  * @param DirectedGraph $graph
  *   The graph on which to perform the depth-first search.
  * @param DepthFirstVisitorInterface $visitor
  *   The visitor object to use during the traversal.
  * @param object|\SplDoublyLinkedList $start
  *   A vertex, or vertices, to use as start points for the traversal. There
  *   are a few sub-behaviors here:
  *     - If an SplDoublyLinkedList, SplQueue, or SplStack is provided, the
  *       traversal will deque and visit vertices contained therein.
  *     - If a single vertex object is provided, it will be the sole
  *       originating point for the traversal.
  *     - If no value is provided, DepthFirst::find_sources() is called to
  *       search the graph for source vertices. These are place into an
  *       SplQueue in the order in which they are discovered, and traversal
  *       is then run over that queue in the same manner as if calling code
  *       had provided a queue directly. This method *guarantees* that all
  *       vertices in the graph will be visited.
  *
  * @throws RuntimeException
  *   Thrown if an invalid $start parameter is provided.
  */
 public static function traverse(DirectedGraph $graph, DepthFirstVisitorInterface $visitor, $start = NULL)
 {
     if ($start === NULL) {
         $queue = self::find_sources($graph, $visitor);
     } else {
         if ($start instanceof \SplDoublyLinkedList) {
             $queue = $start;
         } else {
             if (is_object($start)) {
                 $queue = new \SplDoublyLinkedList();
                 $queue->push($start);
             }
         }
     }
     if ($queue->isEmpty()) {
         throw new RuntimeException('No start vertex or vertices were provided, and no source vertices could be found in the provided graph.', E_WARNING);
     }
     $visiting = new \SplObjectStorage();
     $visited = new \SplObjectStorage();
     $visitor->beginTraversal();
     $visit = function ($vertex) use($graph, $visitor, &$visit, $visiting, $visited) {
         if ($visiting->contains($vertex)) {
             $visitor->onBackEdge($vertex, $visit);
         } else {
             if (!$visited->contains($vertex)) {
                 $visiting->attach($vertex);
                 $visitor->onStartVertex($vertex, $visit);
                 $graph->eachAdjacent($vertex, function ($to) use($vertex, &$visit, $visitor) {
                     $visitor->onExamineEdge($vertex, $to, $visit);
                     $visit($to);
                 });
                 $visitor->onFinishVertex($vertex, $visit);
                 $visiting->detach($vertex);
                 $visited->attach($vertex);
             }
         }
     };
     while (!$queue->isEmpty()) {
         $vertex = $queue->shift();
         $visit($vertex);
     }
     $visitor->endTraversal();
 }
Beispiel #5
0
$list->top();
// bottom()
// 返回第一个节点的值
$list->bottom();
// next()
// 指针移到下一个节点
$list->next();
// prev()
// 指针移到上一个节点, 如果原本指针在第一个,那么前一个节点为-1,并且将无法获得当前值
$list->prev();
// valid()
// 判断该链表是否有更多的值,返回bool
$list->valid();
// isEmpty()
// 判断该链表是否为空链表,返回bool
$list->isEmpty();
// offsetExists($index)
// 判断参索引是否存在,返回bool
$list->offsetExists(2);
// offsetGet($index)
// 返回参数索引的节点值
$list->offsetGet(2);
// offsetSet($index, $newValue)
// 设置参数索引的节点值, $index必须在链表的键范围中。
// 当一个节点用offsetUnset删掉时时,并不能用offsetSet重新给已删掉的节点设置值,只能对当前可见的节点的值进行修改
$list->offsetSet(3, 'value6');
// offsetUnset($index)
// 删除参数索引的节点
$list->offsetUnset(3);
// add($index, $value);
// 对指定的索引新增一个新值。 当一个节点用offsetUnset时,并没有直接删除,该节点还仍然会保存在内存中。用add可以重新给该节点设置值
Beispiel #6
0
 /**
  * Make an iterable that will yield this iterable's values in reverse order. Keys in the
  * original iterable will be preserved.
  *
  * This iterable will be consumed in its entirety when `reverse()` is called.
  *
  * Examples
  *
  *   Crankshaft::Iter(['a', 'b', 'c', 'd', 'e'])->reverse()->to_array()
  *   // => [4 => 'e', 3 => 'd, 2 => 'c', 1 => 'b', 0 => 'a']
  *
  * Returns an Iterable.
  */
 public function reverse()
 {
     $keys = new \SplDoublyLinkedList();
     $values = new \SplDoublyLinkedList();
     foreach ($this as $key => $value) {
         $keys->push($key);
         $values->push($value);
     }
     $reversed = [];
     while (!$values->isEmpty()) {
         $reversed[$keys->pop()] = $values->pop();
     }
     return Crankshaft::Iter($reversed);
 }
<?php

// Create a new Doubly Linked List
$dll = new SplDoublyLinkedList();
var_dump($dll->isEmpty());
<?php

// Create a new Doubly Linked List
$dll = new SplDoublyLinkedList();
// Add some items to the list
$dll->push(1);
$dll->push(2);
$dll->push(3);
//var_dump($dll);
var_dump($dll->isEmpty("test"));
Beispiel #9
0
 /**
  * Notify observers, this method will store the return value from each 
  * observer in a SplDoublyLinkedList. If any one of the observers returns
  * 'false', then the notification stops with the last value of the List
  * being 'false'.
  * 
  * @param string $event
  * @param mixed $args
  * @return SplDoublyLinkedList | return values from the observers
  */
 public function notify($event, $args = null)
 {
     $args = $args instanceof \stdClass && $args->name === 'static' ? $args->args : array_slice(func_get_args(), 1);
     $list = new SplDoublyLinkedList();
     foreach ($this->getObservers($event) as $observer) {
         $list->push($this->invoke($event, $observer, $args));
         if (!$list->isEmpty() && $list->top() === false) {
             break;
         }
     }
     return $list;
 }
Beispiel #10
0
 /**
  * Checks whether the list is empty or not.
  *
  * @return bool True if the list is empty, false otherwise.
  * @link http://www.php.net/manual/en/spldoublylinkedlist.isempty.php
  */
 public function isEmpty()
 {
     return $this->tokens->isEmpty();
 }