tools/oxenstored: Backport find_opt/update from 4.06
authorEdwin Török <edvin.torok@citrix.com>
Fri, 8 Jan 2021 11:57:37 +0000 (11:57 +0000)
committerAndrew Cooper <andrew.cooper3@citrix.com>
Fri, 22 Jan 2021 18:01:34 +0000 (18:01 +0000)
We are currently on OCaml 4.02 as minimum version.  To make the followup
optimizations compile backport these functions from OCaml 4.06.

This implementation is less efficient than the one in the 4.06 standard
library which has access to the internals of the Map.

Signed-off-by: Edwin Török <edvin.torok@citrix.com>
Acked-by: Christian Lindig <christian.lindig@citrix.com>
tools/ocaml/xenstored/stdext.ml
tools/ocaml/xenstored/trie.ml

index e1567c4dfa85d1290df9ed25b678a522fbe5eaf3..116920917a5b105233925b697d7cfbe9596a33c3 100644 (file)
@@ -50,6 +50,25 @@ module Filename = struct
                cmd :: args |> List.map quote |> String.concat " "
 end
 
+module Map = struct
+       module Make(Ord: Map.OrderedType) = struct
+
+       include Map.Make(Ord)
+
+       let find_opt k t = try Some (find k t) with Not_found -> None
+
+       let update k f t =
+               let r = find_opt k t in
+               let r' = f r in
+               match r, r' with
+               | None, None -> t
+               | Some _, None -> remove k t
+               | Some r, Some r' when r == r' -> t
+               | _, Some r' -> add k r' t
+
+       end
+end
+
 module String = struct include String
 
 let of_char c = String.make 1 c
index dc42535092d24b5657ae5022fb22465f63ffdb67..f513f4e6087d286eb226066bc587e6e308b8cac8 100644 (file)
@@ -13,6 +13,8 @@
  * GNU Lesser General Public License for more details.
  *)
 
+open Stdext
+
 module Node =
 struct
        type ('a,'b) t =  {