1 (* Copyright (C) 2004, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
26 exception History_failure
28 class ['a] history size =
29 let unsome = function Some x -> x | None -> assert false in
32 val history_data = Array.create (size + 1) None
34 val mutable history_hd = 0 (* rightmost index *)
35 val mutable history_cur = 0 (* current index *)
36 val mutable history_tl = 0 (* leftmost index *)
38 method private is_empty = history_data.(history_cur) = None
40 method push (status: 'a) =
42 history_data.(history_cur) <- Some status
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
51 method undo = function
52 | 0 -> unsome history_data.(history_cur)
53 | steps when steps > 0 ->
55 if history_cur >= history_tl then
56 history_cur - history_tl
58 history_cur + (size - history_tl)
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
68 method redo = function
69 | 0 -> unsome history_data.(history_cur)
70 | steps when steps > 0 ->
72 if history_hd >= history_cur then
73 history_hd - history_cur
75 history_hd + (size - history_cur)
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