PHP based on bidirectional loop queue to achieve history of the forward and backward functions, queue history _php Tutorial

Source: Internet
Author: User

PHP is based on the bidirectional loop queue to achieve history of the forward and backward functions, queue history


In this paper, we describe the forward and backward functions of PHP based on the bidirectional cyclic queue implementation history. Share to everyone for your reference. Specific as follows:

To achieve a record of the operation history of the function

1. And undo, anti-undo feature similar to a feature. (Implement the forward and backward of the operation)
2. and Discuz Forum to view posts (you can go back to review the posts, as well as post view history)
3. Logic and Windows Explorer address bar forward and backward functions are the same.

Based on this requirement, a data structure is implemented. Write a generic class, called The history class.

"The principle and the clock are similar. When instantiating an object, you can construct a ring with a length of n (which can be fixed as long as you want).

Then integrate the various operations. Forward, backward, insert, modify insert.

Class can construct an array. Or an object is constructed by passing in an array parameter. After each operation, the array after the operation can be obtained. The finished data can be saved in the right way, depending on your needs. Put it in cookie,session, or serialize it, or convert it to JSON data in a database, or in a file. Easy to use next time.

For ease of expansion, more data is stored. Each individual piece of data is also an array record.
For example, expand as needed: Array (' path ' = ' d:/www/', ' SSS ' =>value)

By the way, write your own debug variable with a file.

1. PR () can format and highlight output variables. PR ($arr), PR ($arr, 1) is output after exiting.
2. Debug_out () is used to output multiple variables. The default is exit.
3. Debug_out ($_get,$_server,$_post, $arr);

history.class.php file:

<?php include ' debug.php ';/*** History Action class * Pass in or construct an array. Form: Array (' History_num ' =>20,//Queue node total number ' first ' =>0,//start position, starting from 0.  Array index value ' last ' =>0,//end position, starting from 0.  ' Back ' =>0,//How many steps backwards from first position, difference.    ' History ' =>array (//array, store operation queue.  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) {//array is empty. Constructs a loop queue.      $history =array ();      for ($i =0; $i < $num; $i + +) {Array_push ($history, Array (' path ' = = ')); } $array =array (' history_num ' = $num, ' first ' =>0,//start position ' last ' =>0,//end position ' back ' =&    Gt;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) {//loop n a value.    Similar to the clock loop. Return ($i + $n) < $this->history_num?  ($i + $n):($i + $n-$this->history_num); } function Prevnum ($i, $n =1) {//Loop previous value I.    Rolls back N positions. Return ($i-$n) >=0?     ($i-$n): ($i-$n + $this->history_num); } function minus ($i, $j) {//Clockwise two pips only, I-j return ($i > $j)?  ($i-$j):($i-$j + $this->history_num);    } function Gethistory () {//returns an array for saving or serializing operations. Return Array (' history_num ' = $this->history_num, ' first ' = ' $this->first, ' last ' =&G T  $this->last, ' back ' = $this->back, ' history ' and $this->history);      } function Add ($path) {if ($this->back!=0) {//has a back-action record in case of insertion.      $this->goedit ($path);    Return      if ($this->history[0][' path ']== ') {//Just construct, do not add one. The first not to move forward $this->history[$this->first][' path ']= $path;    Return   }else{$this->first= $this->nextnum ($this->first);//First move forward   $this->history[$this->first][' path ']= $path;    The IF ($this->first== $this->last) {//Start position meets the terminating position $this->last= $this->nextnum ($this->last);//The end position is moved forward.    }} function GoBack () {//Returns the address of the N step back from first.    $this->back+=1;    The maximum number of back steps is the difference between the starting point and the end point (clockwise difference) $mins = $this->minus ($this->first, $this->last);    if ($this->back >= $mins) {//retreat to the last point $this->back= $mins;    } $pos = $this->prevnum ($this->first, $this->back);  return $this->history[$pos [' Path '];    } function GoNext () {//step backward from First n step.    $this->back-=1;    if ($this->back<0) {//retreat to last point $this->back=0;  } return $this->history[$this->prevnum ($this->first, $this->back) [' Path ']; The function Goedit ($path) {//goes back to a point without advancing but modifying.    The firs value is the last value.    $pos = $this->minus ($this->first, $this->back);    $pos = $this->nextnum ($pos);//Next $this->history[$pos] [' Path ']= $path;    $this->first= $pos;  $this->back=0; }//Whether you can back FunctiOn Isback () {if ($this->back < $this->minus ($this->first, $this->last)) {return ture;  } return false;    }//Can advance function Isnext () {if ($this->back>0) {return true;  } return false; }}//test Code. $hi =new History (Array (), 6);//pass in an empty array, initialize the array construct.  for ($i =0; $i <8; $i + +) {$hi->add (' s '. $i); }PR ($hi->goback ());p R ($hi->goback ());p R ($hi->goback ());p R ($hi->gonext ());p R ($hi->gonext ());p R ($ Hi->gonext ());p R ($hi->gonext ()), $hi->add (' asdfasdf '), $hi->add (' asdfasdf2 ');p R ($hi->gethistory () ); $ss =new history ($hi->gethistory ());//Direct array construction. $ss->add (' asdfasdf '), $ss->goback ();p R ($ss->gethistory ());? >

debug.php file:

<?php/** * Gets the name of the variable * eg hello= "123" gets the SS string */function get_var_name (& $aVar) {foreach ($GLOBALS as $key = = $var)    {if ($aVar = = $GLOBALS [$key] && $key! = "ARGC") {return $key;  }}}/** * Format output variable, or Object * @param mixed $var * @param boolean $exit */function PR ($var, $exit = False) {Ob_start (); $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 ();//buffered output to $out variable $out =preg_replace ('/' (. *) "/', '"'.' \\1 '. ' "', $out);//Highlight string variable $out =preg_replace ('/=\> (. *)/', ' = = '. ''.' \\1 '. '', $out);//Highlight and the value after $out =preg_replace ('/\[(. *) \]/', '['.' \\1 '. ']', $out);//Highlight variable $from = Array (', ' (', ') ', ' = = '); $to = Array (', '(', ')', '= ='); $out =str_replace ($from, $to, $out); $keywords =array (' array ', ' int ', ' String ', ' class ', ' object ', ' null ');//keyword highlighting $keywords _to= $keywords; foreach ($keywords as $key = + $val) {$keywords _to[$key] = '. $val. '; } $out =str_replace ($keywords, $keywords _to, $out); echo $style. '
'. Get_var_name ($var). ' = '. $out. '
'; if ($exit) exit;//is true Then exit}/** * Debug output variable, the value of the object. * Parameter any (any type of variable) * @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;}? >

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1044848.html www.bkjia.com true http://www.bkjia.com/PHPjc/1044848.html techarticle PHP based on the bidirectional loop queue to achieve the history of the forward and backward functions, queue History This article describes the PHP based on the bidirectional loop queue to achieve historical record of the forward and backward functions. ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.