From 0d1f9ff76aa6df3f17edd2d73c76ab444fec8528 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Mon, 2 Jan 2017 17:56:04 +0100 Subject: Corrected some issues with odf documents --- sheet.ml | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) (limited to 'sheet.ml') diff --git a/sheet.ml b/sheet.ml index b604c03..38a45d7 100755 --- a/sheet.ml +++ b/sheet.ml @@ -1,7 +1,9 @@ +module Option = Tools.Option + type cell = int * int type search = [ - | `Pattern of ScTypes.result + | `Pattern of ScTypes.result option | `Next | `Previous ] @@ -17,7 +19,7 @@ module Raw = struct type content = { expr : Expression.t; (** The expression *) - value : ScTypes.result; (** The content evaluated *) + value : ScTypes.result option; (** The content evaluated *) sink : Cell.Set.t; (** All the cell which references this one *) } @@ -29,7 +31,7 @@ module Raw = struct (** An empty cell which does contains nothing *) let empty_cell = { expr = Expression.load @@ UTF8.empty; - value = ScTypes.Result ScTypes.Undefined; + value = None; sink = Cell.Set.empty; } @@ -37,7 +39,7 @@ module Raw = struct let get_value id t = begin try (Map.find id t).value - with Not_found -> ScTypes.Result ScTypes.Undefined + with Not_found -> None end let get_expr id t = begin @@ -48,7 +50,7 @@ module Raw = struct (** Extract a value from a reference. This function is given to the evaluator for getting the values from a reference. *) - let get_ref from t ref = begin + let get_ref from t ref : ScTypes.types option ScTypes.Refs.range = begin let extract_values = begin function | ScTypes.Result v -> v @@ -56,21 +58,27 @@ module Raw = struct end in ScTypes.Refs.collect ref - |> ScTypes.Refs.map (fun coord -> extract_values (get_value coord t)) + |> ScTypes.Refs.map (fun coord -> Option.map extract_values (get_value coord t)) end (** Update the value for the given cell. Evaluate the new expression and compare it with the previous value. - @return the map updated if the result differ. + @return Some map if the map has been updated *) let update cell content t = begin let new_val = Expression.eval content.expr (get_ref cell t) in - if not (ScTypes.Result.(=) new_val content.value) then - Some (Map.add cell { content with value = new_val } t) - else - (* If there is no changes, do not update the map *) - None + match content.value with + | None -> + (* If the previous value wasn't defined, update the map *) + Some (Map.add cell { content with value = Some new_val } t) + | Some old_value -> + (* If the previous value was defined, update only if both differs *) + if not (ScTypes.Result.(=) new_val old_value) then + Some (Map.add cell { content with value = Some new_val } t) + else + (* If there is no changes, do not update the map *) + None end (** Parse all the successors from [init] and call [f] for each of them. @@ -176,7 +184,7 @@ module Raw = struct else let f cell t = { cell with expr = expression ; - value = Expression.eval expression (get_ref id t) + value = Some (Expression.eval expression (get_ref id t)) } in add_element id f t end @@ -186,7 +194,7 @@ module Raw = struct let f cell t = { cell with expr = expr ; - value = Expression.eval expr (get_ref id t) + value = Some (Expression.eval expr (get_ref id t)) } in add_element id f t end @@ -206,8 +214,14 @@ module Raw = struct try (Map.find id t).sink with Not_found -> Cell.Set.empty + (** Fold over each defined value *) let fold f a t = begin - Map.fold (fun key content a -> f a key (content.expr, content.value)) t a + Map.fold (fun key content a -> + match content.value with + | None -> a + | Some x -> + f a key (content.expr, x) + ) t a end end -- cgit v1.2.3