Php implements the forward and backward functions of historical records based on bidirectional Cyclic Queues. queue history record-PHP Tutorial

Source: Internet
Author: User
Php implements the forward and backward functions of historical records based on bidirectional cyclic queues, and records historical records of queues. Php implements the forward and backward functions of historical records based on two-way cyclic queue. This article describes how php implements the forward and backward functions of historical records based on bidirectional cyclic queue. Php implements the forward and backward functions of historical records based on bidirectional cyclic queues.

This example describes how php implements the forward and backward functions of historical records based on bidirectional cyclic queues. Share it with you for your reference. The details are as follows:

To enable the record operation history function

1. a function similar to the undo or undo function. (Implement forward and backward operations)
2. log on to the discuz Forum and view the post. (you can view the post that 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 );

History. class. php file:

<? Php include 'debug. php';/*** history operation class * to input or construct an array. Format: array ('History _ num' => 20, // total number of queue nodes 'first' => 0, // start position, starting from 0. Array index value 'last' => 0, // end point, starting from 0. 'Back' => 0, // The number of steps from the first position, difference value. 'History '=> array (// array, which stores operation queues. 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) {// The array is empty. construct a cyclic 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' => 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 value under the loop. Similar to clock loops. Return ($ I + $ n) <$ this-> history_num? ($ I + $ n) :( $ I + $ n-$ this-> history_num);} function prevNum ($ I, $ n = 1) {// The value I on the loop. Return N locations. Return ($ I-$ n)> = 0? ($ I-$ n): ($ I-$ n + $ this-> history_num);} function minus ($ I, $ j) {// only two points clockwise, i-j return ($ I> $ j )? ($ I-$ j) :( $ I-$ j + $ this-> history_num);} function getHistory () {// returns an array for saving or serialization operations. 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) {// Insert if there is a rollback operation record. $ This-> goedit ($ path); return;} if ($ this-> history [0] ['path'] = '') {// just constructed, do not add one. do not move the first place forward $ this-> history [$ this-> first] ['path'] = $ path; return ;} else {$ this-> first = $ this-> nextNum ($ this-> first ); // move the first place forward $ this-> history [$ this-> first] ['path'] = $ path ;} if ($ this-> first = $ this-> last) {// start position and end position $ this-> last = $ this-> nextNum ($ this-> last); // move forward the end position.} Function goback () {// return the N-step back from first. $ This-> back + = 1; // The maximum number of backward steps is the difference from the start point to the end point (clockwise) $ mins = $ this-> minus ($ this-> first, $ this-> last); if ($ this-> back >=$ mins) {// return to the last point $ this-> back = $ mins ;} $ pos = $ this-> prevNum ($ this-> first, $ this-> back); return $ this-> history [$ pos] ['path'];} function gonext () {// step N after the first step. $ This-> back-= 1; if ($ this-> back <0) {// return to the last point $ this-> back = 0 ;} return $ this-> history [$ this-> prevNum ($ this-> first, $ this-> back)] ['path'];} function goedit ($ path) {// move back to a certain point. instead of moving forward, it is modified. The firs value is the final 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 the function isback () {if ($ this-> back <$ this-> minus ($ this-> first, $ this-> last )) {return ture;} return false;} // whether the function isnext () {if ($ this-> back> 0) {return true;} return false ;}} // test the code. $ Hi = new history (array (), 6); // input an empty array to initialize the array structure. For ($ I = 0; $ I <8; $ I ++) {$ hi-> add ('s '. $ I);} pr ($ hi-> goback ()); pr ($ hi-> gonext ()); pr ($ hi-> gonext (); $ hi-> add ('asdfasdf'); $ hi-> add ('asdfasdf2 '); pr ($ hi-> getHistory (); $ ss = new history ($ hi-> getHistory (); // you can directly construct it using arrays. $ Ss-> add ('asdfasdf '); $ ss-> goback (); pr ($ ss-> getHistory ();?>

Debug. php file:

<? Php/*** get variable name ** eg hello = "123" get ss string */function get_var_name (& $ aVar) {foreach ($ GLOBALS as $ key => $ var) {if ($ aVar = $ GLOBALS [$ key] & $ key! = "Argc") {return $ key ;}}/*** format the output variable, or the 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 (); // buffer the output to the $ out variable $ out = preg_replace ('/"(. *)"/','"'.' \ 1 '.'"', $ Out); // The highlighted string variable $ out = preg_replace ('/=\> (. *)/',' => '.''.' \ 1 '.'', $ Out); // highlight => the following value $ out = preg_replace ('/\ [(. *) \]/','['.' \ 1 '.']', $ Out); // The highlighted variable $ from = array ('',' (',') ',' => '); $ to = array ('','(',')','=>'); $ Out = str_replace ($ from, $ to, $ out); $ keywords = array ('array', 'int', 'string', 'class ', 'object', 'null'); // keyword highlighted $ 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; // exit if it is true}/*** debug the output variable, the value of the object. * Any number of parameters (any type of variables) * @ 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 will help you with php programming.

The example in this article describes how php implements the forward and backward functions of historical records based on bidirectional cyclic queues ....

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.