Blame browser/jg_memo.ml

Packit bd2e5d
(*************************************************************************)
Packit bd2e5d
(*                                                                       *)
Packit bd2e5d
(*                         OCaml LablTk library                          *)
Packit bd2e5d
(*                                                                       *)
Packit bd2e5d
(*            Jacques Garrigue, Kyoto University RIMS                    *)
Packit bd2e5d
(*                                                                       *)
Packit bd2e5d
(*   Copyright 1999 Institut National de Recherche en Informatique et    *)
Packit bd2e5d
(*   en Automatique and Kyoto University.  All rights reserved.          *)
Packit bd2e5d
(*   This file is distributed under the terms of the GNU Library         *)
Packit bd2e5d
(*   General Public License, with the special exception on linking       *)
Packit bd2e5d
(*   described in file ../../../LICENSE.                                 *)
Packit bd2e5d
(*                                                                       *)
Packit bd2e5d
(*************************************************************************)
Packit bd2e5d
Packit bd2e5d
(* $Id$ *)
Packit bd2e5d
Packit bd2e5d
type ('a, 'b) assoc_list =
Packit bd2e5d
    Nil
Packit bd2e5d
  | Cons of 'a * 'b * ('a, 'b) assoc_list
Packit bd2e5d
Packit bd2e5d
let rec assq key = function
Packit bd2e5d
    Nil -> raise Not_found
Packit bd2e5d
  | Cons (a, b, l) ->
Packit bd2e5d
      if key == a then b else assq key l
Packit bd2e5d
Packit bd2e5d
let fast ~f =
Packit bd2e5d
  let memo = ref Nil in
Packit bd2e5d
  fun key ->
Packit bd2e5d
    try assq key !memo
Packit bd2e5d
    with Not_found ->
Packit bd2e5d
      let data = f key in
Packit bd2e5d
      memo := Cons(key, data, !memo);
Packit bd2e5d
      data