-exception History_failure
-
- (** shell like phrase history *)
-class history size =
- let size = size + 1 in
- let decr x = let x' = x - 1 in if x' < 0 then size + x' else x' in
- let incr x = (x + 1) mod size in
- object
- val data = Array.create size ""
- val mutable hd = 0 (* insertion point *)
- val mutable tl = -1 (* oldest inserted item *)
- val mutable cur = -1 (* current item for the history *)
- method add s =
- data.(hd) <- s;
- if tl = -1 then tl <- hd;
- hd <- incr hd;
- if hd = tl then tl <- incr tl;
- cur <- hd
- method previous =
- if cur = tl then raise History_failure;
- cur <- decr cur;
- data.(cur)
- method next =
- if cur = hd then raise History_failure;
- cur <- incr cur;
- if cur = hd then "" else data.(cur)
- end
-