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/
28 exception History_failure
30 class ['a] history size =
31 let unsome = function Some x -> x | None -> assert false in
34 val history_data = Array.create (size + 1) None
36 val mutable history_hd = 0 (* rightmost index *)
37 val mutable history_cur = 0 (* current index *)
38 val mutable history_tl = 0 (* leftmost index *)
40 method private is_empty = history_data.(history_cur) = None
42 method push (status: 'a) =
44 history_data.(history_cur) <- Some status
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
53 method undo = function
54 | 0 -> unsome history_data.(history_cur)
55 | steps when steps > 0 ->
57 if history_cur >= history_tl then
58 history_cur - history_tl
60 history_cur + (size - history_tl)
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
70 method redo = function
71 | 0 -> unsome history_data.(history_cur)
72 | steps when steps > 0 ->
74 if history_hd >= history_cur then
75 history_hd - history_cur
77 history_hd + (size - history_cur)
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