PHP implementation of bidirectional loop queue---(to realize the history of the forward and backward functions)

Source: Internet
Author: User
Tags rewind
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 post (you can go back to view the post, as well as Post view history)
    3. the logical 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. End of Operation The data can be saved in the right way according to 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);
  1. Include ' debug.php ';
  2. /**
  3. * History Operation Class
  4. * Pass in or construct an array. Shaped like:
  5. Array
  6. Total number of ' history_num ' =>20,//queue nodes
  7. ' First ' =>0,//starting position, starting from 0. Array index value
  8. ' Last ' =>0,//end position, starting from 0.
  9. ' Back ' =>0,//the number of steps backwards from first position, the difference.
  10. ' History ' =>array (//array, which holds the operation queue.
  11. Array (' path ' = ' d:/'),
  12. Array (' path ' = ' d:/www/'),
  13. Array (' path ' = ' e:/'),
  14. Array (' path ' = '/home/')
  15. ......
  16. )
  17. )
  18. */
  19. Class history{
  20. var $history _num;
  21. var $first;
  22. var $last;
  23. var $back;
  24. var $history =array ();
  25. function __construct ($array =array (), $num =12) {
  26. if (! $array) {//array is empty. Constructs a circular queue.
  27. $history =array ();
  28. for ($i =0; $i < $num; $i + +) {
  29. Array_push ($history, Array (' path ' = '));
  30. }
  31. $array =array (
  32. ' History_num ' = $num,
  33. ' First ' =>0,//start position
  34. ' Last ' =>0,//end position
  35. ' Back ' =>0,
  36. ' History ' and $history
  37. );
  38. }
  39. $this->history_num= $array [' History_num '];
  40. $this->first= $array [' first '];
  41. $this->last= $array [' last '];
  42. $this->back= $array [' Back '];
  43. $this->history= $array [' History '];
  44. }
  45. function Nextnum ($i, $n =1) {//loop n a value. Similar to the clock loop.
  46. Return ($i + $n) < $this->history_num? ($i + $n):($i + $n-$this->history_num);
  47. }
  48. function Prevnum ($i, $n =1) {//Loop previous value I. Rolls back N positions.
  49. Return ($i-$n) >=0? ($i-$n): ($i-$n + $this->history_num);
  50. }
  51. function minus ($i, $j) {//Clockwise two points only, i-j
  52. Return ($i > $j)? ($i-$j):($i-$j + $this->history_num);
  53. }
  54. function Gethistory () {//returns an array for saving or serializing operations.
  55. Return Array (
  56. ' History_num ' = $this->history_num,
  57. ' First ' = $this->first,
  58. ' Last ' = $this->last,
  59. ' Back ' = $this->back,
  60. ' History ' = $this->history
  61. );
  62. }
  63. function Add ($path) {
  64. if ($this->back!=0) {//has a back action record, insert.
  65. $this->goedit ($path);
  66. Return
  67. }
  68. if ($this->history[0][' path ']== ') {//Just construct, do not add one. First not move forward
  69. $this->history[$this->first][' path ']= $path;
  70. Return
  71. }else{
  72. $this->first= $this->nextnum ($this->first);//First move forward
  73. $this->history[$this->first][' path ']= $path;
  74. }
  75. if ($this->first== $this->last) {//Start and end position meet
  76. $this->last= $this->nextnum ($this->last);//The end position is moved forward.
  77. }
  78. }
  79. function GoBack () {//returns the address of n step back from first.
  80. $this->back+=1;
  81. The maximum number of back steps is the difference between the starting point and the end point (clockwise difference)
  82. $mins = $this->minus ($this->first, $this->last);
  83. if ($this->back >= $mins) {//rewind to last point
  84. $this->back= $mins;
  85. }
  86. $pos = $this->prevnum ($this->first, $this->back);
  87. return $this->history[$pos [' Path '];
  88. }
  89. function GoNext () {//step backward from First n step.
  90. $this->back-=1;
  91. if ($this->back<0) {//rewind to last point
  92. $this->back=0;
  93. }
  94. return $this->history[$this->prevnum ($this->first, $this->back) [' Path '];
  95. }
  96. function Goedit ($path) {//back to a point, without advance but modified. The firs value is the last value.
  97. $pos = $this->minus ($this->first, $this->back);
  98. $pos = $this->nextnum ($pos);//Next
  99. $this->history[$pos [' Path ']= $path;
  100. $this->first= $pos;
  101. $this->back=0;
  102. }
  103. Whether you can back
  104. function Isback () {
  105. if ($this->back < $this->minus ($this->first, $this->last)) {
  106. return ture;
  107. }
  108. return false;
  109. }
  110. Is it possible to move forward
  111. function Isnext () {
  112. if ($this->back>0) {
  113. return true;
  114. }
  115. return false;
  116. }
  117. }
  118. Test the code.
  119. $hi =new History (Array (), 6);//pass in an empty array, initialize the array construct.
  120. for ($i =0; $i <8; $i + +) {
  121. $hi->add (' s '. $i);
  122. }
  123. PR ($hi->goback ());
  124. PR ($hi->goback ());
  125. PR ($hi->goback ());
  126. PR ($hi->gonext ());
  127. PR ($hi->gonext ());
  128. PR ($hi->gonext ());
  129. PR ($hi->gonext ());
  130. $hi->add (' asdfasdf ');
  131. $hi->add (' asdfasdf2 ');
  132. PR ($hi->gethistory ());
  133. $ss =new History ($hi->gethistory ());//Direct array construction.
  134. $ss->add (' asdfasdf ');
  135. $ss->goback ();
  136. PR ($ss->gethistory ());
  137. ?>
Copy Code
  1. /**
  2. * Get the name of the variable
  3. * EG hello= "123" gets the SS string
  4. */
  5. Function Get_var_name (& $aVar) {
  6. foreach ($GLOBALS as $key = $var)
  7. {
  8. if ($aVar = = $GLOBALS [$key] && $key! = "ARGC") {
  9. return $key;
  10. }
  11. }
  12. }
  13. /**
  14. * Format output variable, or object
  15. * @param mixed $var
  16. * @param boolean $exit
  17. */
  18. function Pr ($var, $exit = False) {
  19. Ob_start ();
  20. $style = " ;
  21. if (Is_array ($var)) {
  22. Print_r ($var);
  23. }
  24. else if (Is_object ($var)) {
  25. echo Get_class ($var). " Object ";
  26. }
  27. else if (Is_resource ($var)) {
  28. Echo (string) $var;
  29. }
  30. else{
  31. echo Var_dump ($var);
  32. }
  33. $out = Ob_get_clean ();//buffered output to $out variable
  34. $out =preg_replace ('/' (. *) "/', '" '. ' \\1 '. ' "', $out);//Highlight string variable
  35. $out =preg_replace ('/=\> (. *)/', ' = '. ' '.' \\1 '.', $out);//Highlight and the value behind
  36. $out =preg_replace ('/\[(. *) \]/', '['. ' \\1 '. ' ]', $out);//Highlight variable
  37. $from = Array (', ' (', ') ', ' = = ');
  38. $to = Array (', '(', ')', ' == ');
  39. $out =str_replace ($from, $to, $out);
  40. $keywords =array (' array ', ' int ', ' String ', ' class ', ' object ', ' null ');//keyword highlighting
  41. $keywords _to= $keywords;
  42. foreach ($keywords as $key = $val)
  43. {
  44. $keywords _to[$key] = ". $val.";
  45. }
  46. $out =str_replace ($keywords, $keywords _to, $out);
  47. echo $style. '
    '. Get_var_name ($var). ' = '. $out. '
    ';
  48. if ($exit) exit;//is true Then exit
  49. }
  50. /**
  51. * Debug output variable, the value of the object.
  52. * Any parameter (any type of variable)
  53. * @return Echo
  54. */
  55. function Debug_out () {
  56. $avg _num = Func_num_args ();
  57. $avg _list= Func_get_args ();
  58. Ob_start ();
  59. for ($i =0; $i < $avg _num; $i + +) {
  60. PR ($avg _list[$i]);
  61. }
  62. $out =ob_get_clean ();
  63. Echo $out;
  64. Exit
  65. }
  66. ?>
Copy Code
  • Related Article

    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.