]> matita.cs.unibo.it Git - helm.git/blob - components/tactics/history.ml
tagged 0.5.0-rc1
[helm.git] / components / 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 (* $Id$ *)
27
28 exception History_failure
29
30 class ['a] history size =
31   let unsome = function Some x -> x | None -> assert false in
32   object (self)
33
34     val history_data = Array.create (size + 1) None
35
36     val mutable history_hd = 0  (* rightmost index *)
37     val mutable history_cur = 0 (* current index *)
38     val mutable history_tl = 0  (* leftmost index *)
39
40     method private is_empty = history_data.(history_cur) = None
41
42     method push (status: 'a) =
43       if self#is_empty then
44         history_data.(history_cur) <- Some status
45       else begin
46         history_cur <- (history_cur + 1) mod size;
47         history_data.(history_cur) <- Some status;
48         history_hd <- history_cur;  (* throw away fake future line *)
49         if history_hd = history_tl then (* tail overwritten *)
50           history_tl <- (history_tl + 1) mod size
51       end
52
53     method undo = function
54       | 0 -> unsome history_data.(history_cur)
55       | steps when steps > 0 ->
56           let max_undo_steps =
57             if history_cur >= history_tl then
58               history_cur - history_tl
59             else
60               history_cur + (size - history_tl)
61           in
62           if steps > max_undo_steps then
63             raise History_failure;
64           history_cur <- history_cur - steps;
65           if history_cur < 0 then (* fix underflow *)
66             history_cur <- size + history_cur;
67           unsome history_data.(history_cur)
68       | steps (* when steps > 0 *) -> self#redo ~-steps
69
70     method redo = function
71       | 0 -> unsome history_data.(history_cur)
72       | steps when steps > 0 ->
73           let max_redo_steps =
74             if history_hd >= history_cur then
75               history_hd - history_cur
76             else
77               history_hd + (size - history_cur)
78           in
79           if steps > max_redo_steps then
80             raise History_failure;
81           history_cur <- (history_cur + steps) mod size;
82           unsome history_data.(history_cur)
83       | steps (* when steps > 0 *) -> self#undo ~-steps
84
85   end
86