diff options
author | Pierre-Marie Pédrot <pierre-marie.pedrot@inria.fr> | 2016-02-03 16:20:46 +0100 |
---|---|---|
committer | Pierre-Marie Pédrot <pierre-marie.pedrot@inria.fr> | 2016-02-03 16:25:23 +0100 |
commit | e9675e068f9e0e92bab05c030fb4722b146123b8 (patch) | |
tree | 5912374d4085bb410f5ed712328b39e965d0a868 /lib | |
parent | 292e205138ca11c3dce37d48cb34ef4172f6fa06 (diff) |
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.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/cMap.ml | 2 | ||||
-rw-r--r-- | lib/cMap.mli | 3 | ||||
-rw-r--r-- | lib/hMap.ml | 2 |
3 files changed, 7 insertions, 0 deletions
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 = |