]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/history.ml
ocaml 3.09 transition
[helm.git] / helm / ocaml / tactics / history.ml
1 (* Copyright (C) 2004, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 exception History_failure
27
28 class ['a] history size =
29   let unsome = function Some x -> x | None -> assert false in
30   object (self)
31
32     val history_data = Array.create (size + 1) None
33
34     val mutable history_hd = 0  (* rightmost index *)
35     val mutable history_cur = 0 (* current index *)
36     val mutable history_tl = 0  (* leftmost index *)
37
38     method private is_empty = history_data.(history_cur) = None
39
40     method push (status: 'a) =
41       if self#is_empty then
42         history_data.(history_cur) <- Some status
43       else begin
44         history_cur <- (history_cur + 1) mod size;
45         history_data.(history_cur) <- Some status;
46         history_hd <- history_cur;  (* throw away fake future line *)
47         if history_hd = history_tl then (* tail overwritten *)
48           history_tl <- (history_tl + 1) mod size
49       end
50
51     method undo = function
52       | 0 -> unsome history_data.(history_cur)
53       | steps when steps > 0 ->
54           let max_undo_steps =
55             if history_cur >= history_tl then
56               history_cur - history_tl
57             else
58               history_cur + (size - history_tl)
59           in
60           if steps > max_undo_steps then
61             raise History_failure;
62           history_cur <- history_cur - steps;
63           if history_cur < 0 then (* fix underflow *)
64             history_cur <- size + history_cur;
65           unsome history_data.(history_cur)
66       | steps (* when steps > 0 *) -> self#redo ~-steps
67
68     method redo = function
69       | 0 -> unsome history_data.(history_cur)
70       | steps when steps > 0 ->
71           let max_redo_steps =
72             if history_hd >= history_cur then
73               history_hd - history_cur
74             else
75               history_hd + (size - history_cur)
76           in
77           if steps > max_redo_steps then
78             raise History_failure;
79           history_cur <- (history_cur + steps) mod size;
80           unsome history_data.(history_cur)
81       | steps (* when steps > 0 *) -> self#undo ~-steps
82
83   end
84