php基于双向循环队列实现历史记录的前进后退等功能


Posted in PHP onAugust 08, 2015

本文实例讲述了php基于双向循环队列实现历史记录的前进后退等功能。分享给大家供大家参考。具体如下:

为实现一个记录操作历史的功能

1. 和撤销,反撤销功能类似的一个功能。(实现操作的前进后退)
2. 和discuz论坛登录后查看帖子(可以前进后退查看过的帖子,还有帖子查看历史记录)
3. 逻辑和windows资源管理器地址栏前进后退功能一样。

根据这种需要,实现了一个数据结构。写了一个通用的类,暂叫历史记录类吧。

【原理和时钟类似。实例化对象时可以构造长度为N(可以根据需要定长度)个节点的环】

然后整合各种操作。前进、后退、插入、修改插入。

类可以构造一个数组。或者传入数组参数构造一个对象。 每次操作之后可以取得操作后的数组。 操作完的 数据可以根据自己的需要以合适的方式保存。 放在cookie,session里面,或者序列化,或转为json数据保存在数据库里,或者放在文件里面都可以。 方便下一次使用。

为了便于扩展,存放更多的数据。具体每一条数据也是一条数组记录。
比如根据需要进行扩展:array('path'=>'D:/www/','sss'=>value)

顺便贴出,自己写的调试变量用的一个文件。

1. pr()可以格式化并高亮输出变量。pr($arr),pr($arr,1)是输出后退出。
2. debug_out()  用来输出多个变量。默认为退出。
3. debug_out($_GET,$_SERVER,$_POST,$arr);

history.class.php文件:

<?php 
include 'debug.php';
/**
* 历史记录操作类
* 传入或者构造一个数组。形如:
array(
  'history_num'=>20,  //队列节点总共个数
  'first'=>0,     //起始位置,从0开始。数组索引值
  'last'=>0,      //终点位置,从0开始。
  'back'=>0,      //从first位置倒退了多少步,差值。
  'history'=>array(  //数组,存放操作队列。
    array('path'=>'D:/'),
    array('path'=>'D:/www/'),
    array('path'=>'E:/'),
    array('path'=>'/home/')
    ……
  )
)
*/
class history{
  var $history_num;
  var $first;
  var $last;
  var $back;
  var $history=array();
  function __construct($array=array(),$num=12){
    if (!$array) {//数组为空.构造一个循环队列。
      $history=array();
      for ($i=0; $i < $num; $i++) {
        array_push($history,array('path'=>''));
      }
      $array=array(
        'history_num'=>$num,
        'first'=>0,//起始位置
        'last'=>0,//终点位置
        'back'=>0,  
        'history'=>$history
      );
    }    
    $this->history_num=$array['history_num'];
    $this->first=$array['first'];
    $this->last=$array['last'];
    $this->back=$array['back']; 
    $this->history=$array['history'];  
  }
  function nextNum($i,$n=1){//环路下n一个值。和时钟环路类似。
    return ($i+$n)<$this->history_num ? ($i+$n):($i+$n-$this->history_num);
  }
  function prevNum($i,$n=1){//环路上一个值i。回退N个位置。
    return ($i-$n)>=0 ? ($i-$n) : ($i-$n+$this->history_num);   
  }
  function minus($i,$j){//顺时针两点只差,i-j
    return ($i > $j) ? ($i - $j):($i-$j+$this->history_num);
  }
  function getHistory(){//返回数组,用于保存或者序列化操作。
    return array(
      'history_num'=> $this->history_num,
      'first'   => $this->first,     
      'last'    => $this->last,
      'back'    => $this->back,     
      'history'  => $this->history
    );
  }
  function add($path){
    if ($this->back!=0) {//有后退操作记录的情况下,进行插入。
      $this->goedit($path);
      return;
    }    
    if ($this->history[0]['path']=='') {//刚构造,不用加一.首位不前移
      $this->history[$this->first]['path']=$path;
      return;
    }else{
      $this->first=$this->nextNum($this->first);//首位前移
      $this->history[$this->first]['path']=$path;      
    }
    if ($this->first==$this->last) {//起始位置与终止位置相遇
      $this->last=$this->nextNum($this->last);//末尾位置前移。
    }    
  }
  function goback(){//返回从first后退N步的地址。
    $this->back+=1;
    //最大后退步数为起点到终点之差(顺时针之差)
    $mins=$this->minus($this->first,$this->last);
    if ($this->back >= $mins) {//退到最后点
      $this->back=$mins;
    }
    $pos=$this->prevNum($this->first,$this->back);
    return $this->history[$pos]['path'];
  }
  function gonext(){//从first后退N步的地方前进一步。
    $this->back-=1;
    if ($this->back<0) {//退到最后点
      $this->back=0;
    }
    return $this->history[$this->prevNum($this->first,$this->back)]['path'];
  }
  function goedit($path){//后退到某个点,没有前进而是修改。则firs值为最后的值。
    $pos=$this->minus($this->first,$this->back);
    $pos=$this->nextNum($pos);//下一个   
    $this->history[$pos]['path']=$path;
    $this->first=$pos;
    $this->back=0;
  }
  //是否可以后退
  function isback(){
    if ($this->back < $this->minus($this->first,$this->last)) {
      return ture;
    }
    return false;
  }
  //是否可以前进
  function isnext(){
    if ($this->back>0) {
      return true;
    }
    return false;
  }
}
//测试代码。
$hi=new history(array(),6);//传入空数组,则初始化数组构造。
for ($i=0; $i <8; $i++) { 
  $hi->add('s'.$i);  
}
pr($hi->goback());
pr($hi->goback());
pr($hi->goback());
pr($hi->gonext());
pr($hi->gonext());
pr($hi->gonext());
pr($hi->gonext());
$hi->add('asdfasdf');
$hi->add('asdfasdf2');
pr($hi->getHistory());
$ss=new history($hi->getHistory());//直接用数组构造。
$ss->add('asdfasdf');
$ss->goback();
pr($ss->getHistory());
?>

debug.php文件:

<?php 
/**
 * 获取变量的名字
 * eg hello="123" 获取ss字符串
 */
function get_var_name(&$aVar){
  foreach($GLOBALS as $key=>$var)
  {
    if($aVar==$GLOBALS[$key] && $key!="argc"){
      return $key;
    }
  }
}
/**
 * 格式化输出变量,或者对象
 * @param mixed  $var
 * @param boolean $exit
 */
function pr($var,$exit = false){
  ob_start();
  $style='<style>
  pre#debug{margin:10px;font-size:13px;color:#222;font-family:Consolas ;line-height:1.2em;background:#f6f6f6;border-left:5px solid #444;padding:5px;width:95%;word-break:break-all;}
  pre#debug b{font-weight:400;}
  #debug #debug_str{color:#E75B22;}
  #debug #debug_keywords{font-weight:800;color:00f;}
  #debug #debug_tag1{color:#22f;}
  #debug #debug_tag2{color:#f33;font-weight:800;}
  #debug #debug_var{color:#33f;}
  #debug #debug_var_str{color:#f00;}
  #debug #debug_set{color:#0C9CAE;}</style>';
  if (is_array($var)){
    print_r($var);
  }
  else if(is_object($var)){
    echo get_class($var)." Object";
  }
  else if(is_resource($var)){
    echo (string)$var;
  }
  else{
    echo var_dump($var);
  }  
  $out = ob_get_clean();//缓冲输出给$out 变量
  $out=preg_replace('/"(.*)"/','<b id="debug_var_str">"'.'\\1'.'"</b>',$out);//高亮字符串变量
  $out=preg_replace('/=\>(.*)/','=>'.'<b id="debug_str">'.'\\1'.'</b>',$out);//高亮=>后面的值
  $out=preg_replace('/\[(.*)\]/','<b id="debug_tag1">[</b><b id="debug_var">'.'\\1'.'</b><b id="debug_tag1">]</b>',$out);//高亮变量
  $from = array('  ','(',')','=>');
  $to  = array(' ','<b id="debug_tag2">(</i>','<b id="debug_tag2">)</b>','<b id="debug_set">=></b>');
  $out=str_replace($from,$to,$out);  
  $keywords=array('Array','int','string','class','object','null');//关键字高亮
  $keywords_to=$keywords;
  foreach($keywords as $key=>$val)
  {  
    $keywords_to[$key] = '<b id="debug_keywords">'.$val.'</b>';
  }
  $out=str_replace($keywords,$keywords_to,$out); 
  echo $style.'<pre id="debug"><b id="debug_keywords">'.get_var_name($var).'</b> = '.$out.'</pre>';
  if ($exit) exit;//为真则退出
}
/**
 * 调试输出变量,对象的值。
 * 参数任意个(任意类型的变量)
 * @return echo
 */
function debug_out(){
  $avg_num = func_num_args();
  $avg_list= func_get_args();
  ob_start();
  for($i=0; $i < $avg_num; $i++) {
    pr($avg_list[$i]);
  }
  $out=ob_get_clean();
  echo $out;
  exit;
}
?>

希望本文所述对大家的php程序设计有所帮助。

PHP 相关文章推荐
PHPlet在Windows下的安装
Oct 09 PHP
使用无限生命期Session的方法
Oct 09 PHP
Smarty安装配置方法
Apr 10 PHP
改变Apache端口等配置修改方法
Jun 05 PHP
php下使用SimpleXML 处理XML 文件
Feb 27 PHP
PHP常用代码大全(新手入门必备)
Jun 29 PHP
php下连接mssql2005的代码
Jan 17 PHP
解析php获取字符串的编码格式的方法(函数)
Jun 21 PHP
php header功能的使用
Oct 28 PHP
解密ThinkPHP3.1.2版本之独立分组功能应用
Jun 19 PHP
Zend Framework实现将session存储在memcache中的方法
Mar 22 PHP
PHP实现在对象之外访问其私有属性private及保护属性protected的方法
Nov 20 PHP
PHP实现获取文件后缀名的几种常用方法
Aug 08 #PHP
PHP实现多维数组转字符串和多维数组转一维数组的方法
Aug 08 #PHP
Smarty使用自定义资源的方法
Aug 08 #PHP
SESSION存放在数据库用法实例
Aug 08 #PHP
摘自织梦CMS的HTTP文件下载类
Aug 08 #PHP
摘自织梦CMS中的图片处理类
Aug 08 #PHP
PHP模拟asp.net的StringBuilder类实现方法
Aug 08 #PHP
You might like
DOTA2 玩家自创拉野攻略 特色英雄快速成长篇
2020/04/20 DOTA
windows 2008r2+php5.6.28环境搭建详细过程
2019/06/18 PHP
用JavaScript实现UrlEncode和UrlDecode的脚本代码
2008/07/23 Javascript
javascript innerHTML、outerHTML、innerText、outerText的区别
2008/11/24 Javascript
bgsound 背景音乐 的一些常用方法及特殊用法小结
2010/05/11 Javascript
玩转jQuery按钮 请告诉我你最喜欢哪些?
2012/01/08 Javascript
js DOM 元素ID就是全局变量
2012/09/20 Javascript
JavaScript mapreduce工作原理简析
2012/11/25 Javascript
jquery.messager.js插件导致页面抖动的解决方法
2013/07/14 Javascript
Javascript排序算法之合并排序(归并排序)的2个例子
2014/04/04 Javascript
微信小程序 Tab页切换更新数据
2017/01/05 Javascript
基于jQuery封装的分页组件
2017/06/26 jQuery
JavaScript贪吃蛇小组件实例代码
2017/08/20 Javascript
详解vuex之store源码简单解析
2019/06/13 Javascript
VUE使用 wx-open-launch-app 组件开发微信打开APP功能
2020/08/11 Javascript
简单介绍Python的轻便web框架Bottle
2015/04/08 Python
Python实现获取域名所用服务器的真实IP
2015/10/25 Python
详解Python中open()函数指定文件打开方式的用法
2016/06/04 Python
python 搭建简单的http server,可直接post文件的实例
2019/01/03 Python
如何在Python中实现goto语句的方法
2019/05/18 Python
python中pygame安装过程(超级详细)
2019/08/04 Python
python报错: 'list' object has no attribute 'shape'的解决
2020/07/15 Python
Joie官方网上商店:购买服装和女装配饰
2018/06/05 全球购物
岗位职责定义及内容
2013/11/08 职场文书
高二化学教学反思
2014/01/30 职场文书
五星级酒店餐饮部总监的标准岗位职责
2014/02/17 职场文书
解除合同协议书
2014/04/17 职场文书
计算机网络专业求职信
2014/06/05 职场文书
运动会班级口号
2014/06/09 职场文书
入党宣誓大会后的感想
2015/08/10 职场文书
2016创先争优活动党员公开承诺书
2016/03/24 职场文书
导游词之嵊泗列岛
2019/10/30 职场文书
Jupyter notebook 不自动弹出网页的解决方案
2021/05/21 Python
Mysql存储过程、触发器、事件调度器使用入门指南
2022/01/22 MySQL
vue实现移动端div拖动效果
2022/03/03 Vue.js
Python中使用Opencv开发停车位计数器功能
2022/04/04 Python