class virtual ['a] edit_distance first_item second_item = object(ed) val f = (first_item: 'a) val s = (second_item: 'a) val mutable calced = false val mutable matrix = ([|[||]|] : int array array) method private gen_matrix f_size s_size = let matri = Array.create_matrix (s_size + 1) (f_size + 1) 0 in Array.iteri (fun x y -> match x with 0 -> Array.iteri (fun n m -> matri.(x).(n) <- n) y | _ -> matri.(x).(0) <- x ) matri; matri method private trimin x y z = match x,y,z with m,n,o when (m > n) -> if (n < o) then n else o | m,n,o when (m < n) -> if (m < o) then m else o | m,n,o -> if (n < o) then n else o method private update_matrix m d d' cost = let fval = m.(d).((d' - 1)) + 1 in let sec = m.((d - 1)).((d')) + 1 in let third = m.((d - 1)).((d' - 1)) + cost in let newval = ed#trimin fval sec third in m.(d).(d') <- newval method virtual private calc: unit -> unit method distance () = ed#calc (); matrix.((Array.length matrix) - 1).((Array.length matrix.(0)) - 1) end;; class string_edit_distance x y = object(sed) inherit ['a] edit_distance x y method next m n = {< matrix = sed#gen_matrix (String.length m) (String.length n); f = m; s = n; calced = false >} method private calc () = if (not calced) then (Array.iteri (fun ind x -> match ind with 0 -> () | dex -> Array.iteri (fun ind' x' -> match ind' with 0 -> () | dex' -> if (f.[(dex' - 1)] = s.[(dex - 1)]) then sed#update_matrix matrix dex dex' 0 else sed#update_matrix matrix dex dex' 1 ) x) matrix; calced <- true ) initializer matrix <- sed#gen_matrix (String.length x) (String.length y) end;;