From e9675e068f9e0e92bab05c030fb4722b146123b8 Mon Sep 17 00:00:00 2001 From: Pierre-Marie Pédrot Date: Wed, 3 Feb 2016 16:20:46 +0100 Subject: Adding a "get" primitive to map signature. It is similar to find but raises an assertion failure instead of a Not_found when the element is not encountered. Using it will give stronger invariants. --- lib/cMap.ml | 2 ++ lib/cMap.mli | 3 +++ lib/hMap.ml | 2 ++ 3 files changed, 7 insertions(+) (limited to 'lib') diff --git a/lib/cMap.ml b/lib/cMap.ml index d1a32b7aa..4b058380c 100644 --- a/lib/cMap.ml +++ b/lib/cMap.ml @@ -25,6 +25,7 @@ module type ExtS = sig include CSig.MapS module Set : CSig.SetS with type elt = key + val get : key -> 'a t -> 'a val update : key -> 'a -> 'a t -> 'a t val modify : key -> (key -> 'a -> 'a) -> 'a t -> 'a t val domain : 'a t -> Set.t @@ -207,4 +208,5 @@ module Make(M : Map.OrderedType) = struct include Map.Make(M) include MapExt(M) + let get k m = try find k m with Not_found -> assert false end diff --git a/lib/cMap.mli b/lib/cMap.mli index 464dc503b..3ef7fa2c8 100644 --- a/lib/cMap.mli +++ b/lib/cMap.mli @@ -31,6 +31,9 @@ sig module Set : CSig.SetS with type elt = key (** Sets used by the domain function *) + val get : key -> 'a t -> 'a + (** Same as {!find} but fails an assertion instead of raising [Not_found] *) + val update : key -> 'a -> 'a t -> 'a t (** Same as [add], but expects the key to be present, and thus faster. @raise Not_found when the key is unbound in the map. *) diff --git a/lib/hMap.ml b/lib/hMap.ml index 220adc28f..778c366fd 100644 --- a/lib/hMap.ml +++ b/lib/hMap.ml @@ -286,6 +286,8 @@ struct let m = Int.Map.find h s in Map.find k m + let get k s = try find k s with Not_found -> assert false + let split k s = assert false (** Cannot be implemented efficiently *) let map f s = -- cgit v1.2.3