当前位置: 首页>>代码示例>>PHP>>正文


PHP SplStack::rewind方法代码示例

本文整理汇总了PHP中SplStack::rewind方法的典型用法代码示例。如果您正苦于以下问题:PHP SplStack::rewind方法的具体用法?PHP SplStack::rewind怎么用?PHP SplStack::rewind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SplStack的用法示例。


在下文中一共展示了SplStack::rewind方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: inorder

function inorder($t)
{
    $stack = new SplStack();
    $result = array();
    $p = $t;
    while ($stack->valid() || $p) {
        if ($p) {
            $stack->push($p);
            $stack->rewind();
            $p = $p->left;
        } else {
            $t = $stack->pop();
            $result[] = $t->key;
            $stack->rewind();
            $p = $t->right;
        }
    }
    return $result;
}
开发者ID:eltonoliver,项目名称:Algorithms,代码行数:19,代码来源:inorder_traversal.php

示例2: postorder

function postorder($tree)
{
    $lst = array();
    if (!$tree) {
        return $lst;
    }
    $stack = new SplStack();
    $stack->push($tree);
    $stack->rewind();
    $prev = null;
    while ($stack->valid()) {
        $curr = $stack->current();
        // go down the tree.
        //check if current node is leaf, if so, process it and pop stack,
        //otherwise, keep going down
        if (!$prev || @$prev->left->key == @$curr->key || @$prev->right->key == @$curr->key) {
            //prev == null is the situation for the root node
            if ($curr->left) {
                $stack->push($curr->left);
                $stack->rewind();
            } else {
                if ($curr->right) {
                    $stack->push($curr->right);
                    $stack->rewind();
                } else {
                    $stack->pop();
                    $stack->rewind();
                    $lst[] = $curr->key;
                }
            }
            //go up the tree from left node
            //need to check if there is a right child
            //if yes, push it to stack
            //otherwise, process parent and pop stack
        } else {
            if (@$curr->left->key == @$prev->key) {
                if (@$curr->right->key) {
                    $stack->push($curr->right);
                    $stack->rewind();
                } else {
                    $stack->pop();
                    $stack->rewind();
                    $lst[] = $curr->key;
                }
            } else {
                if (@$curr->right->key == @$prev->key) {
                    $stack->pop();
                    $stack->rewind();
                    $lst[] = $curr->key;
                }
            }
        }
        $prev = $curr;
    }
    return $lst;
}
开发者ID:eltonoliver,项目名称:Algorithms,代码行数:56,代码来源:postorder_traversal.php

示例3: log

 /**
  * Logs with an arbitrary level.
  *
  * @param string $level
  * @param string|array $message a textual message, encoded JSON or array to encode into JSON
  * @param array $context context regarding the message, if this is included it will
  *                       added to the message as json or included in the json payload
  * @throws \InvalidArgumentException
  * @return null
  */
 public function log($level, $message, array $context = array())
 {
     $this->connectIfNotConnected();
     if (is_array($message)) {
         $message = json_encode($message);
     }
     if (!is_string($message)) {
         throw new \InvalidArgumentException('the message argument needs to be a string or an array');
     } else {
         $isJson = $this->isJSON($message);
         if ($isJson) {
             $json = json_decode($message, true);
             if ("" != $this->_hostname) {
                 $json["hostname"] = $this->_hostname;
             }
             $json["level"] = $level;
             if (count($context) > 0) {
                 $json["context"] = $context;
             }
             $message = json_encode($json);
         } else {
             $message = strtoupper($level) . " - " . $message;
             if ("" != $this->_hostname) {
                 $message = "hostname={$this->_hostname} - " . $message;
                 if (count($context) > 0) {
                     $message .= " - " . json_encode($context);
                 }
             }
         }
         $this->_writer_stack->rewind();
         while ($this->_writer_stack->valid()) {
             /** @var LogEntriesWriter $writer */
             $writer = $this->_writer_stack->current();
             $message = $writer->log($message, $isJson);
             $this->_writer_stack->next();
         }
         $this->writeToSocket($this->substituteNewline($message) . PHP_EOL);
     }
 }
开发者ID:cbschuld,项目名称:logentries,代码行数:49,代码来源:LogEntries.php

示例4: dealWithChildDepartmentNotMatch

 /**
  * 匹配最小子科室未匹配到科室信息的情况
  * @author gaoqing
  * 2015年11月2日
  * @param SplStack $notMatchDepartmentStack 未匹配的子科室栈
  * @param array $matchArr 已匹配的父科室数组
  * @return int 更新后的最小子科室id 
  */
 private function dealWithChildDepartmentNotMatch($notMatchDepartmentStack, $matchArr)
 {
     $currentDepartmentID = 0;
     //都未匹配时:
     $pid = 0;
     //部分匹配时:
     if (!empty($matchArr)) {
         $pid = isset($matchArr[0]) ? isset($matchArr[0][0]) ? $matchArr[0][0]['id'] : 0 : 0;
     }
     $notMatchDepartmentStack->rewind();
     while ($notMatchDepartmentStack->valid()) {
         if ($notMatchDepartmentStack->isEmpty()) {
             break;
         }
         $departmentAndLevel = $notMatchDepartmentStack->pop();
         //向 wd_keshi_test 表中,插入科室信息
         $currentDepartmentID = $pid = $this->insertKeshiInfo($departmentAndLevel, $pid);
     }
     return $currentDepartmentID;
 }
开发者ID:VampireMe,项目名称:Common_PHP,代码行数:28,代码来源:MatchData.php


注:本文中的SplStack::rewind方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。