Php implements two-way cyclic queue --- (implements the forward and backward functions of history records)-php Tutorial

Source: Internet
Author: User
Php implements two-way cyclic queue-(implements functions such as forward and backward history records)
To enable the record operation history function


  1. This function is similar to the undo or undo function. (Implement forward and backward operations)
  2. View the post after logging on to the discuz Forum (you can view the post you have viewed before and after, and view the history of the post)
  3. The logic is the same as that in the address bar of windows resource manager.


According to this need, a data structure is implemented. I wrote a general class, which is called the history class.
[The principle is similar to the clock. When instantiating an object, you can construct a ring with a length of N (you can set the length as needed) nodes]
Then integrate various operations. Forward, backward, insert, modify insert.

Class can construct an array. Or input an array parameter to construct an object. After each operation, you can obtain the array after the operation.The operated data can be saved as needed. It can be stored in cookies, sessions, or serialized, or converted into json data in the database, or in files. Easy to use next time.

To facilitate expansion and store more data. Each piece of data is also an array record.
For example, extend the value as needed: array ('path' => 'd:/www/', 'SS' => value)

----------------------------------------------------------------------------


By the way, a file is used for debugging variables written by myself.
  1. Pr () can be formatted and highlighted. Pr ($ arr), pr ($ arr, 1) is output and then exited.
  2. Debug_out () is used to output multiple variables. Exit by default.
  3. Debug_out ($ _ GET, $ _ SERVER, $ _ POST, $ arr );

  1. Include 'debug. php ';
  2. /**
  3. * History operation
  4. * Input or construct an array. Shape:
  5. Array (
  6. 'History _ num' => 20, // total number of queue nodes
  7. 'First' => 0, // start position, starting from 0. Array index value
  8. 'Last' => 0, // The end point, starting from 0.
  9. 'Back' => 0, // The number of steps from the first position, difference value.
  10. 'History '=> array (// array, which stores operation queues.
  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) {// The array is empty. construct a cyclic 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 point
  35. 'Back' => 0,
  36. 'History '=> $ 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) {// A value of n under the loop. Similar to clock loops.
  46. Return ($ I + $ n) <$ this-> history_num? ($ I + $ n) :( $ I + $ n-$ this-> history_num );
  47. }
  48. Function prevNum ($ I, $ n = 1) {// The value I on the loop. Return N locations.
  49. Return ($ I-$ n)> = 0? ($ I-$ n): ($ I-$ n + $ this-> history_num );
  50. }
  51. Function minus ($ I, $ j) {// only two points clockwise, 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) {// Insert if there is a rollback operation record.
  65. $ This-> goedit ($ path );
  66. Return;
  67. }
  68. If ($ this-> history [0] ['path'] = '') {// just constructed, do not add one. do not move forward first
  69. $ This-> history [$ this-> first] ['path'] = $ path;
  70. Return;
  71. } Else {
  72. $ This-> first = $ this-> nextNum ($ this-> first); // move the first
  73. $ This-> history [$ this-> first] ['path'] = $ path;
  74. }
  75. If ($ this-> first ==$ this-> last) {// The starting position and ending position
  76. $ This-> last = $ this-> nextNum ($ this-> last); // move the end forward.
  77. }
  78. }
  79. Function goback () {// return the N-step back from first.
  80. $ This-> back + = 1;
  81. // The maximum number of backward steps is the difference from the start point to the end point (clockwise)
  82. $ Mins = $ this-> minus ($ this-> first, $ this-> last );
  83. If ($ this-> back >=$ mins) {// return to the last vertex
  84. $ This-> back = $ mins;
  85. }
  86. $ Pos = $ this-> prevNum ($ this-> first, $ this-> back );
  87. Return $ this-> history [$ pos] ['path'];
  88. }
  89. Function gonext () {// step N after the first step.
  90. $ This-> back-= 1;
  91. If ($ this-> back <0) {// return to the last vertex
  92. $ This-> back = 0;
  93. }
  94. Return $ this-> history [$ this-> prevNum ($ this-> first, $ this-> back)] ['path'];
  95. }
  96. Function goedit ($ path) {// move back to a certain point. instead of moving forward, it is modified. The firs value is the final 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 the backend is allowed
  104. Function isback (){
  105. If ($ this-> back <$ this-> minus ($ this-> first, $ this-> last )){
  106. Return ture;
  107. }
  108. Return false;
  109. }
  110. // Whether it can be moved 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); // input an empty array to initialize the array structure.
  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 (); // you can directly construct it using arrays.
  134. $ Ss-> add ('asdfasdf ');
  135. $ Ss-> goback ();
  136. Pr ($ ss-> getHistory ());
  137. ?>

  1. /**
  2. * Get the variable name
  3. * Eg hello = "123" get 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 the 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 (); // buffer the output to the $ out variable
  34. $ Out = preg_replace ('/"(.*)"/','"'.' \ 1 '.'"', $ Out); // highlight string variables
  35. $ Out = preg_replace ('/=\> (. *)/', '=> '.''.' \ 1 '.'', $ Out); // The value after Highlight =>
  36. $ Out = preg_replace ('/\ [(. *) \]/','['.' \ 1 '.']', $ Out); // highlight the 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; // exit if it is true.
  49. }
  50. /**
  51. * Debug the output variable, the object value.
  52. * Any number of parameters (any type of variables)
  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. ?>

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.