package core-and-more

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file hash_memoizer.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
open Core
open Util

module type UnfixedHashFunction = sig
  module Arg : Data
  module Key : Hashtbl.Key
  module Result : Data
  val f : (Arg.t -> Result.t) unfixed
  val extract_key : Arg.t -> Key.t
end

module FixHashMemoizerOf(F:UnfixedHashFunction) = struct
  let result_storage : (F.Key.t,F.Result.t) Hashtbl.t = Hashtbl.create (module F.Key)

  let rec evaluate
      (x:F.Arg.t)
    : F.Result.t =
    let key = F.extract_key x in
    begin match Hashtbl.find result_storage key with
      | None ->
        let y = F.f evaluate x in
        Hashtbl.add_exn ~key:key ~data:y result_storage;
        y
      | Some y -> y
    end
end