aboutsummaryrefslogtreecommitdiffstats
path: root/lib/kernel/Merkle.ml
diff options
context:
space:
mode:
Diffstat (limited to 'lib/kernel/Merkle.ml')
-rw-r--r--lib/kernel/Merkle.ml33
1 files changed, 20 insertions, 13 deletions
diff --git a/lib/kernel/Merkle.ml b/lib/kernel/Merkle.ml
index b8599d5..be4618b 100644
--- a/lib/kernel/Merkle.ml
+++ b/lib/kernel/Merkle.ml
@@ -37,6 +37,15 @@ module type TREE = functor (S : Abstract.Storage.STORAGE) (K : KEY) -> sig
val remove : S.transaction -> K.t -> node -> (node, Concepts.Condition.condition) result
val lookup : S.transaction -> K.t -> node -> (address option, Concepts.Condition.condition) result
+ val foldm_left :
+ ?from:K.t ->
+ ?until:(K.t -> bool) ->
+ S.transaction ->
+ ('c -> K.t -> address -> ('c, Concepts.Condition.condition) result) ->
+ 'c ->
+ node ->
+ ('c, Concepts.Condition.condition) result
+
val fold_left :
?from:K.t ->
?until:(K.t -> bool) ->
@@ -349,19 +358,19 @@ functor
let drop i ft =
if i < BatFingerTree.size ft then snd (BatFingerTree.split_at ft i) else BatFingerTree.empty
- let fold_left ?from ?(until = fun _ -> false) tx f acc node =
+ let foldm_left ?from ?(until = fun _ -> false) tx f acc node =
let open Utilities.Result in
let rec walk from acc = function
| Leaf {keys; values} ->
let i = Option.fold ~none:0 ~some:(fun key -> fst (lookup1 keys key)) from in
Utilities.FingerTree.zip (drop i keys) (drop i values)
- |> BatFingerTree.fold_left
+ |> Utilities.FingerTree.foldm_left
(fun step (k, v) ->
match step with
- | `Continue acc -> if until k then `Stop acc else `Continue (f acc k v)
- | `Stop _ -> step )
+ | `Continue acc when until k -> Ok (`Stop acc)
+ | `Continue acc -> f acc k v |> Result.map (fun acc -> `Continue acc)
+ | `Stop _ -> Ok step )
(`Continue acc)
- |> Result.ok
| Trunk {keys; children} ->
let i =
Option.fold ~none:0
@@ -384,6 +393,8 @@ functor
in
walk from acc node |> Result.map (function `Continue acc | `Stop acc -> acc)
+ let fold_left ?from ?until tx f = foldm_left ?from ?until tx (fun acc k v -> Ok (f acc k v))
+
let keys tx node =
fold_left tx (fun acc k _ -> BatFingerTree.snoc acc k) BatFingerTree.empty node
@@ -459,6 +470,8 @@ module type INTERFACE = functor (S : Abstract.Storage.STORAGE) (K : KEY) (V : VA
val mem : S.transaction -> K.t -> node -> (bool, Concepts.Condition.condition) result
val fold_left :
+ ?from:K.t ->
+ ?until:(K.t -> bool) ->
S.transaction ->
('c -> K.t -> V.t -> 'c) ->
'c ->
@@ -518,14 +531,8 @@ functor
let mem tx k node = Tree.lookup tx k node |> Result.map Option.is_some
- let fold_left tx f acc node =
- Tree.fold_left tx
- (fun acc k addr ->
- let* acc = acc in
- let* v = retrieve tx addr in
- Ok (f acc k v) )
- (Ok acc) node
- |> Result.join
+ let fold_left ?from ?until tx f =
+ Tree.foldm_left ?from ?until tx (fun acc k addr -> retrieve tx addr |> Result.map (f acc k))
let iter tx f node = fold_left tx (fun _ k v -> f k v |> ignore) () node
let keys = Tree.keys